【发布时间】:2016-06-17 09:42:05
【问题描述】:
我有一个 ObservableCollection 项目,其中一个属性是 bool。 当我将数据网格的 itemsSource 设置为 ObservableCollection 时,它会自动生成带有 bool 属性的复选框列的列。
我想知道如何在代码中勾选复选框,假设我们是否有标记全部选项?
我尝试将 ObservableCollection 记录属性值更新为 true,但它无助于更新 UI。
请帮忙。
[编辑:下面的代码按照答案中的建议工作] 我的班级如下
public class InvoiceDoc : INotifyPropertyChanged
{
private bool _Selected;
[DisplayName("Selected")]
public bool Selected
{
get { return _Selected; }
set { _Selected = value; this.OnPropertyChanged(); }
}
[DisplayName("Date")]
public DateTime DocDate { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged !=null)
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
datagrid如下
<DataGrid x:Name="dgInvoices" Margin="32,110,32,59" AutoGeneratingColumn="dgInvoices_AutoGeneratingColumn"/>
设置ItemsSource如下
docs = new ObservableCollection<InvoiceDoc>(); ;
dgInvoices.ItemsSource = docs;
一旦在集合中设置值,我希望网格会自动选中复选框。
【问题讨论】: