Is it possible to disable individual list box items in WPF? It should appear greyed out and the user should not be possible to select it.
A:
a short program to show you what to do, it compares the Listbox items with a List<> and if the listbox item is not found in it..it gets disabled :D
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: ( i had it run on window's 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 element from listbox exists in the list of data.. it will be active..else it will be flagged inactive..
{ if (LBI.Content.ToString() == data_list[x].ToString())
{
flag_active = true;
}
}
if (flag_active == false)
{
// deactivate element in listbox
LBI.IsEnabled = false;
}
flag_active = false;
}
}
Madi D.
2009-09-08 08:49:43