【发布时间】:2011-10-18 07:48:04
【问题描述】:
我在 WPF 应用程序中使用 Caliburn Micro 作为 MVVM 框架。我几乎没有问题如何选择数据网格控件中的所有复选框。每个数据网格行都有复选框。
我绑定了 List 的 datagrid 属性类型。
型号:
public class Bill : INotifyPropertyChanged
{
public string CellPhoneNo
{
get { return _cellPhoneNo; }
set
{
_cellPhoneNo = value;
NotifyPropertyChanged("CellPhoneNo");
}
}
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
视图模型:
public IList<Bill> TmobileBill
{
get
{
return _tmobileBill;
}
set
{
_tmobileBill = value;
NotifyOfPropertyChange(()=>TmobileBill);
}
}
查看:
<Controls:DataGrid ItemsSource="{Binding Path= TmobileBill,
Mode=OneWay,
UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource FinalBillsView_CallsDataGrid}"
Grid.Row="0"
CanUserResizeRows="False">
<Controls:DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Controls:DataGridRow}}}"/>
</Grid>
</DataTemplate>
</Controls:DataGrid.RowHeaderTemplate>
<Controls:DataGrid.Columns>
<Controls:DataGridTextColumn IsReadOnly="True"
CellStyle="{StaticResource FinalBillsView_DataGrid_CellStyle}"
Binding="{Binding Path=CellPhoneNo}"
HeaderStyle="{StaticResource FinalBillsView_DataGridColHeaderStyle}"
Header="Cell phone No"/>
</Controls:DataGrid.Columns>
</Controls:DataGrid>
在 datragrid 行的数据模板中,我绑定了 checbox 的属性 IsChecked 属性 IsSelected 来自 Bill 类。
<Controls:DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Controls:DataGridRow}}}"/>
</Grid>
</DataTemplate>
</Controls:DataGrid.RowHeaderTemplate>
问题是如果我为列表中的所有项目设置属性 IsSelected 为 true。
foreach (var row in TmobileBill)
{
row.IsSelected = true;
}
视图中的复选框未选中。问题的根源是什么?
谢谢。
【问题讨论】:
-
您使用的是什么类型的控件? (数据网格)
标签: wpf select mvvm datagrid checkbox