gpt4 book ai didi

c# - 如何检查特定项目已选中或未选中

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:30 24 4
gpt4 key购买 nike

我在 wpf 中创建了复选框,它是从互联网上获得的。我想看看哪个项目被选中或未被选中。知道如何做到这一点这是代码

public class CheckedListItem
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsChecked { get; set; }
public string Email { get; set; }
}

用法

List<CheckedListItem> AvailablePresentationObjects = new List<CheckedListItem>();
CheckedListItem item = new CheckedListItem();
for (int i = 0; i < 10; i++)
{
item = new CheckedListItem();
item.Id = i;
item.Name = i.ToString();
item.IsChecked = false;
AvailablePresentationObjects.Add(item);

}
list.ItemsSource = AvailablePresentationObjects;

XMAL

<ListBox x:Name="list"  Margin="3,277,0,0" Height="234" VerticalAlignment="Top" Selec
tionMode="Extended">
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<my:RibbonCheckBox Label="{Binding Name}" IsChecked="{Binding IsChecked}" />
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
</ListBox>

取自这里 Checked ListBox

问题是

How to implement property change so that i can know which item was checked and which was unchecked

My Code solution

最佳答案

您可以获得selected items的收藏: list.SelectedItems。您可以将每个项目转换到 CheckedListItem 并检查该项目是否已选中。如果你想处理属性变化,你应该实现接口(interface) INotifyPropertyChangedCheckedListItem 类中

INotifyPropertyChanged 示例:将其添加到您的类中并在属性中调用 OnPropertyChanged:

    private boolean _isChecked;
public boolean IsChecked
{
get { return _isChecked; }
set
{
_isChecked= value;
OnPropertyChanged("IsChecked");
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}

关于c# - 如何检查特定项目已选中或未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com