【问题标题】:Is it possible to disable individual list box items in WPF?是否可以在 WPF 中禁用单个列表框项?
【发布时间】:2009-09-08 07:59:01
【问题描述】:

是否可以在 WPF 中禁用单个列表框项?它应该显示为灰色,并且用户应该无法选择它。

【问题讨论】:

  • 您知道如果您发现正确答案真的有用,您可以投票赞成吗? :)

标签: .net wpf listbox


【解决方案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;
}

【讨论】:

    【解决方案2】:

    当然是 - 并且得到了回答 here

    【讨论】:

      猜你喜欢
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 2015-07-10
      相关资源
      最近更新 更多