Is it possible to disable individual list items in WPF?

Is it possible to disable individual list items in WPF? It should look gray and the user should not select it.

+3
source share
2 answers

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 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;
}
+1
source

It certainly is - and the answer was given here .

0
source

Source: https://habr.com/ru/post/1717120/


All Articles