【发布时间】:2009-09-08 07:59:01
【问题描述】:
是否可以在 WPF 中禁用单个列表框项?它应该显示为灰色,并且用户应该无法选择它。
【问题讨论】:
-
您知道如果您发现正确答案真的有用,您可以投票赞成吗? :)
是否可以在 WPF 中禁用单个列表框项?它应该显示为灰色,并且用户应该无法选择它。
【问题讨论】:
这是一个简短的程序,向您展示该怎么做。它将Listbox 项与List<> 进行比较,如果在其中找不到列表框项,它将被禁用。
<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>
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;
}
【讨论】:
当然是 - 并且得到了回答 here。
【讨论】: