Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF DataGrid:scolling导致调用控制事件_C#_Wpf_Wpfdatagrid - Fatal编程技术网

C# WPF DataGrid:scolling导致调用控制事件

C# WPF DataGrid:scolling导致调用控制事件,c#,wpf,wpfdatagrid,C#,Wpf,Wpfdatagrid,我目前在C#应用程序中使用WPF数据网格,其中每行包含一个复选框、一个组合框和一个文本框。然后,为CheckBox.Checked、CheckBox.Unchecked和ComboBox.SelectionChanged事件注册一个方法。启用扩展选择后,我希望将控件的更改反映到所有选定行(例如,如果复选框未选中,则应针对每个选定行) 当使用控件并且一切正常时,该方法按预期调用。我的问题是,在调用DataGrid时也会调用该事件,这给我带来了一些麻烦。关于这一点,我的第一个想法是,一些行被虚拟化,

我目前在C#应用程序中使用WPF数据网格,其中每行包含一个复选框、一个组合框和一个文本框。然后,为CheckBox.Checked、CheckBox.Unchecked和ComboBox.SelectionChanged事件注册一个方法。启用扩展选择后,我希望将控件的更改反映到所有选定行(例如,如果复选框未选中,则应针对每个选定行)

当使用控件并且一切正常时,该方法按预期调用。我的问题是,在调用DataGrid时也会调用该事件,这给我带来了一些麻烦。关于这一点,我的第一个想法是,一些行被虚拟化,并且在加载它们时调用事件,但我不确定如何验证这一点

我想简单地忽略这些事件,我使用eventArg来确定所做的更改并将其反映在选定的单元格上。但在滚动时,这会导致一些随机行为

加载时调用事件的是虚拟化行吗?我是否可以检测到这种行为并忽略这些事件

以下是我的DataGrid定义:

<DataGrid AutoGenerateColumns="False" Height="364" HorizontalAlignment="Left" Margin="6,40,0,0" Name="dataGrid" VerticalAlignment="Top" Width="628" CanUserResizeColumns="True" SelectionMode="Extended" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserAddRows="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Column 1">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Name="dataGrid_checkBox" IsChecked="false" Checked="OnDataGridEvent" Unchecked="OnDataGridEvent" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Column 2">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox Name="dataGrid_comboBox" SelectionChanged="OnDataGridEvent">
                        <ComboBoxItem Content="1" IsSelected="True" />
                        <ComboBoxItem Content="2" />
                        <ComboBoxItem Content="3" />
                        <ComboBoxItem Content="4" />
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

感谢您的帮助

不要使用ComboBoxItem
绑定到列表
不要使用SelectionChanged事件-只需使用集合

struct NameBool
{
    private string name;
    private bool selected;
    public string Name
    {
        get { return name; }
        set
        {
            if (name == value) return;
            name = value;
        }
    }
    public bool Selected
    {
        get { return selected; }
        set
        {
            if (selected == value) return;  // this is to ignore a no change
            selected = value;
        }
    }
    public NameBool(string Name, bool Selected) { name = Name; selected = Selected;}
}

我不知道如何设置可以取代我的事件,我需要知道什么时候发生了变化。。。此外,有时是我的复选框调用滚动事件。这就是设置。更改值时调用set。把你在比赛中所做的事情放在场景中。将IsSelected双向绑定到选定的属性。因为scroll调用事件,所以您不想使用偶数。你有试过这个答案吗?谢谢你的评论,我一开始没明白。这个解决方案非常有效!