【问题标题】:Select all checkboxes in datagrid rows from view model从视图模型中选择数据网格行中的所有复选框
【发布时间】: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


【解决方案1】:
  1. 尝试将IList&lt;Bill&gt;更改为ObservableCollection&lt;Bill&gt;
  2. 尝试使用简单绑定&lt;CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"/&gt;

出于调试目的,与 CheckBox 一起定义下一个控件以查看实际绑定到 RowItem 的内容:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多