简体   繁体   English

DataGrid 单行复选框正在选择所有行

[英]DataGrid a single row checkbox is selecting all rows

I am having this issue implementing a checkbox per row.我在每行实现一个复选框时遇到了这个问题。 What happens is that when I click on a single checkbox it then checks/unchecks all other checkboxes in my datagrid.发生的情况是,当我单击一个复选框时,它会选中/取消选中我的数据网格中的所有其他复选框。 I think what may be happening is that I am recreating a checkbox with the same default id.我认为可能发生的事情是我正在重新创建一个具有相同默认 ID 的复选框。 maybe.也许。

Here is my xaml and how I am creating my datagrid with my problem column:这是我的 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>

In my view model this is how I have my observable collection and my checkbox在我看来 model 这就是我拥有可观察集合和复选框的方式

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>();

When I click on the checkbox the property is marked as changed but I am seeing it check all other checkboxes.当我单击复选框时,该属性被标记为已更改,但我看到它检查了所有其他复选框。 Can someone tell me what am I doing wrong?有人可以告诉我我做错了什么吗? Many thanks in advance.提前谢谢了。

You bind all CheckBoxes to the same IsPetChecked source property.您将所有CheckBoxes绑定到相同的IsPetChecked源属性。

If you want to be able to check the CheckBoxes on each row individually, each Pet object in the ItemsSource should have its own IsPetChecked (or IsChecked ) property that you then bind to just like you bind to the PetName property:如果您希望能够单独检查每一行上的CheckBoxesItemsSource中的每个Pet object 都应该有自己的IsPetChecked (或IsChecked )属性,然后您将其绑定到就像绑定到PetName属性一样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM