【问题标题】:DataGrid a single row checkbox is selecting all rowsDataGrid 单行复选框正在选择所有行
【发布时间】:2019-11-19 15:04:21
【问题描述】:

我在每行实现一个复选框时遇到了这个问题。发生的情况是,当我单击一个复选框时,它会选中/取消选中我的数据网格中的所有其他复选框。我认为可能发生的事情是我正在重新创建一个具有相同默认 ID 的复选框。也许。

这是我的 xaml 以及我如何使用问题列创建数据网格:

<DataGrid
            ItemsSource="{Binding Pets, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            SelectedItem="{Binding SelectedPet, Mode=TwoWay}"
            AutoGenerateColumns="False"
                HorizontalAlignment="Left"
                BorderThickness="1"
                CanUserAddRows="False"
                CanUserDeleteRows="False"
                RowHeaderWidth="0"
                MinRowHeight = "25"
                ColumnHeaderHeight="30"
                HorizontalGridLinesBrush="Black"
                VerticalGridLinesBrush="Black"
                Foreground="Black" 
                CanUserSortColumns="True"
                SelectionMode="Single"
                RowStyle="{Binding Mode=OneWay, Source={StaticResource DefaultRowStyle}}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Selection" SortMemberPath="Station" MinWidth="20">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" >
                                <CheckBox IsChecked="{Binding Path=DataContext.IsPetChecked, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>
                                <TextBlock Text="{Binding PetName, Mode=OneWay}"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <!--MORE Columns below-->
            </DataGrid.Columns>
        </DataGrid>

在我的视图模型中,这就是我拥有可观察集合和复选框的方式

public bool IsPetChecked
{
    get
    {
        return _isPetChecked;
    }
    set
    {
        if (_isPetChecked != value)
        {
            _isPetChecked = value;
            RaisePropertyChanged(nameof(IsPetChecked));
        }
   }
}
private bool _isPetChecked = false;

/// <summary>
/// Stores the collection of pets
/// </summary>
public ObservableCollection<Pet> Pets
{
    get
    {
        return _pets;
    }
    set
    {
        _pets = value;
        RaisePropertyChanged(nameof(Pets));
    }
}
private ObservableCollection<Pet> _pets = new ObservableCollection<Pet>();

当我点击复选框时,属性被标记为已更改,但我看到它选中了所有其他复选框。有人可以告诉我我做错了什么吗?非常感谢。

【问题讨论】:

    标签: c# wpf checkbox


    【解决方案1】:

    您将所有CheckBoxes 绑定到同一个IsPetChecked 源属性。

    如果您希望能够单独检查每一行上的CheckBoxes,则ItemsSource 中的每个Pet 对象都应该有自己的IsPetChecked(或IsChecked)属性,然后您将其绑定到就像你绑定到PetName 属性一样:

    <CheckBox IsChecked="{Binding Path=IsChecked}"/>
    

    【讨论】:

    • 好的,所以我制作了一个新模型(类)并将其命名为 PetSummary。在这个类中,我放置了 IsChecked 属性和包含其余属性(名称、年龄、品种)的 Pet 模型(类),但由于某种原因,现在我遇到了以下问题: - 我的数据网格中的 SelectedItem 属性没有更新.
    • @user2529011:您是否将SelectedPet 属性的类型更改为PetSummary
    猜你喜欢
    • 2011-11-21
    • 2018-08-03
    • 2011-04-19
    • 1970-01-01
    • 2021-12-16
    • 2021-05-08
    • 2010-10-19
    • 1970-01-01
    相关资源
    最近更新 更多