c# - 在 C# ListView Checkbox 中使用数据绑定获取 CheckBox 内容
问题描述
来自 python,需要将我制作的一些旧应用程序翻译成 C#。我有一个带有复选框的 ListView。XAML
<ListView Name ="spored" SelectionMode="Multiple" HorizontalAlignment="Left" VerticalAlignment="Stretch" Margin="10,10,10,10" Width="750">
<ListView.View>
<GridView>
<GridViewColumn Header="data">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding CheckedShow, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
在代码中,我拥有填充此 Listview 所需的一切,并且效果很好。
public class Show
{
public bool CheckedShow { get; set; } //listview checkbox
public string Name { get; set; } //listview checkbox content
}
public List<Show> showList = new List<Show>();
private void ShowSpored_Click(object sender, RoutedEventArgs e)
{
//here i have some foreeach loop that populates showList
foreach(...)
{
.
.
.
showList.Add(new Show() { CheckedShow = false, Name = infoKey });
}
//after loop i populate Listview with
spored.ItemsSource = showList;
}
现在我需要实现并且不知道以下是如何实现的。填充列表时,我需要在检查的内容上运行一些代码。我想我需要用 INotifyPropertyChanged.PropertyChanged 事件来做到这一点,但对于我这个菜鸟,我似乎无法设法获得检查的内容......
public class Content : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
.
.
.
}
请帮忙!!!
解决方案
弄清楚了。必须将此添加到cs:
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public event PropertyChangedEventHandler PropertyChanged;
然后创建一个这样的类:
public class Show: INotifyPropertyChanged
{
private bool _IsChecked;
private string _Name;
public bool IsChecked
{
get { return _IsChecked; }
set
{
if (_IsChecked != value)
{
_IsChecked = value;
OnPropertyChanged("IsChecked");
if (_IsChecked == true)
{
Console.WriteLine(_Name + "do some shit");
}
else
{ Console.WriteLine("do some other shit"); }
}
}
}
public string Name
{
get
{
return _Name;
}
set
{
if (_Name != value)
{
_Name = value;
}
}
}
推荐阅读
- python - 未创建输出文件
- php - 如何将 PHP 定义的表中的值提交到 php 页面以进行进一步处理?
- docker - Docker 容器没有运行,即使 -d
- azerothcore - 运行 worldserver 时出错:“000.vmtree”丢失或指向错误版本的 vmap 文件。使用最新版本的 vmap_assembler.exe 重做 vmap
- python - Python:.pop 在同一行和不同行之间的行为不同
- java - Null Dereference - 使用 HashMap.EntrySet() 加强扫描
- android - 在服务之间创建应用内(客户端)通信的最佳实践是什么?
- mysql - MYSQL如何映射这些join结果?
- excel - 需要根据工作表单元格中的唯一 ID 运行宏
- java - 调色板中缺少 GridBagLayout