是否可以禁用 WPF 中的单个列表框项目?它应该显示为灰色并且用户应该无法选择它。
最佳答案
这是一个简短的程序,向您展示该怎么做。它比较 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;
}
关于.net - 是否可以禁用 WPF 中的单个列表框项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1392589/