Here is a short program to show you what to do. It compares items Listboxwith List<>, and if the list item is not found in it, it is disabled.
XAML Code:
<Grid x:Name="LayoutRoot">
<ListBox x:Name="listbox_xaml" HorizontalAlignment="Left" Margin="52,53,0,0" VerticalAlignment="Top" Width="157" Height="141">
<ListBoxItem>Fred</ListBoxItem>
<ListBoxItem>Joe</ListBoxItem>
<ListBoxItem>Mandy</ListBoxItem>
</ListBox>
</Grid>
C # code behind: (start on window load event)
bool flag_active = false;
List<string> data_list = new List<string>();
data_list.Add("Fred");
data_list.Add("Mandy");
for(int i = 0;i<listbox_xaml.Items.Count; i++)
{
ListBoxItem LBI = (ListBoxItem) listbox_xaml.Items[i];
for(int x = 0 ; x < data_list.Count ; x++)
{
if (LBI.Content.ToString() == data_list[x].ToString())
{
flag_active = true;
}
}
if (flag_active == false)
{
LBI.IsEnabled = false;
}
flag_active = false;
}
source
share