【问题标题】:Update a Listview column when checkbox is checked选中复选框时更新 Listview 列
【发布时间】:2017-04-04 11:12:17
【问题描述】:

我想在同一行中检查复选框时更新列表视图中的列。复选框是一行的一部分。

查看:

复选框:

<CheckBox Tag="{Binding ID}" Checked="chbAccount_checked" Unchecked="chbAccount_Unchecked"/>

需要更新的列中的文本块:

 <TextBlock Text="{Binding MyColumnValue }" />

代码背后:

private void chbAccount_checked(object sender, RoutedEventArgs e)
{
    CheckBox chb = sender as CheckBox;
    int Id= Convert.ToInt16(chb.Tag);
    ViewModel.UpdateColumnValue(Id);
    myListView.ItemsSource = ViewModel.Accounts;
    this.myListView.UpdateLayout();              
}

视图模型:

public void UpdateColumnValue(int Id)
{
    foreach(var a in Accounts)
    {
        if(a.ID == Id)
        {
            a.MyColumnValue = "Yes";
        }
    }
}

帐户列表正在更新,但列表视图未在列中显示修改后的值。我尝试通过 listview.item.refresh() 刷新 listview。没用。

请帮忙。谢谢!

【问题讨论】:

  • 您已经在使用绑定。为什么不更新绑定的属性?如果你实现 INotifyPropertyChanged 你只需要更新你的 MyColumnValue 属性。这是 MVVM 模式的优势之一。
  • 我不熟悉 INotifyPropertyChanged 。浏览了一些教程。我想我现在可以处理这个了。非常感谢你。 :)
  • “是的”它正在工作:) :) :)
  • 欢迎您:)

标签: wpf listview checkbox checked


【解决方案1】:

为什么不将CheckBoxIsChecked 属性绑定到视图模型的bool 源属性,并处理在视图模型类中设置MyColumnValue 属性的逻辑:

<CheckBox Tag="{Binding ID}" IsChecked="{Binding IsChecked}"/>

private bool _icChecked;
public bool IsChecked
{
    get { return _icChecked; ; }
    set { _icChecked; = value; NotifyPropertyChanged(); UpdateColumnValue(Convert.ToInt32(ID)); }
}

确保视图模型类正确实现INotifyPropertyChanged接口。

【讨论】:

  • 我使用了 INotifyPropertyChanged 并且它有效。谢谢..! :)
猜你喜欢
  • 2019-02-02
  • 2019-03-21
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
相关资源
最近更新 更多