简体   繁体   中英

WPF DataGrid Automatically selecting checkbox bug?

I have a DataGrid and loading some data of custom type. Also have a Checkbox column . When checkbox is checked current row data is taken by following code. For first checkbox it's fine.

Then i check another checkbox and it gets checked but when i scroll through data i see some other rows checkbox checked automatically ! And when again i go up the check box that i selected first time is now unchecked and some other is selected automatically !

The code i am using for checked Event.

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    var selectedOrder = MyDataGrid.SelectedItem as AutoOrderClass;
    if (selectedOrder != null)
    {
        var order = SelectedOrdersList.FirstOrDefault(x => x.VendorName == selectedOrder.VendorName);
        if (order == null)
        {
            OrdersCount++;
            TotalOrders.Text = "Total Orders : " + OrdersCount;
        }

        SelectedOrdersList.Add(selectedOrder);
    }
}   

And this is Xaml

<DataGrid x:Name="MyDataGrid" VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True"
          AutoGenerateColumns="False"
          HorizontalScrollBarVisibility="Auto" Height="{Binding ActualHeight, ElementName=DataGridContainerPanel}">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn x:Name="CheckBoxColumn" Header="Select">
            <DataGridCheckBoxColumn.CellStyle>
                <Style>
                    <EventSetter Event="CheckBox.Checked" Handler="CheckBox_Checked"/>
                    <EventSetter Event="CheckBox.Unchecked" Handler="CheckBox_Unchecked"/>
                </Style>
            </DataGridCheckBoxColumn.CellStyle>
        </DataGridCheckBoxColumn>
        <DataGridTextColumn Header="Item Name" Width="120" Binding="{Binding Name}"></DataGridTextColumn>
        <DataGridTextColumn Header="Description" IsReadOnly="True" Width="*" Binding="{Binding Description}"></DataGridTextColumn>
        <DataGridTextColumn Header="VendorName" IsReadOnly="True" Width="*" Binding="{Binding VendorName}"></DataGridTextColumn>
        <DataGridTextColumn Header="Quantity In Hand" Width="100" Binding="{Binding StockQuantity}"></DataGridTextColumn>
        <DataGridTextColumn Header="Order QTY" Width="70" Binding="{Binding OrderQuantity,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}"></DataGridTextColumn>
        <DataGridTextColumn Header="Unit Price" Width="80" Binding="{Binding UnitPrice,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}"></DataGridTextColumn>
        <DataGridTextColumn Header="Total Price" Width="70" Binding="{Binding TotalPrice,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}"></DataGridTextColumn>
    </DataGrid.Columns>

</DataGrid>  

And here is the picture of what's happening !
First Pic: all fine only first row selected

Second Pic : Select another BUT See third column was auto checked When i scroll down i see some others are also got checked

3rd Pic : now i scroll up and see the first check box was auto unchecked how ? 在此输入图像描述

在此输入图像描述 在此输入图像描述

正如@SajithSageer所说,为DataGrid设置EnableRowVirtualization= false可以解决这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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