在WPF中,是否可以禁用单个列表框项目?

6
在WPF中,是否有可能禁用单个列表框项目?它应该呈灰色,并且用户不应该能够选择它。

你知道如果你发现某个答案真的很有用,你可以给它点赞吗? :) - Madi D.
2个回答

1
这是一个简短的程序,展示了该如何操作。它将Listbox中的项目与List<>进行比较,如果在列表框中找不到该项目,则将其禁用。

XAML 代码:

<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#代码后台:(在窗口加载事件上运行)

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;
}

0

当然是的 - 并且已经在这里得到了回答。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接