为什么在DataGridTemplateColumn中检查我的CheckBox之前的命令?

时间:2014-09-23 18:01:33

标签: c# wpf xaml datagrid

常规的DataGridCheckBoxColumn似乎不允许命令,因此我决定使用带有复选框的DataGridTemplateColumn。问题是我的命令在选中或取消选中复选框之前被触发了为什么会发生这种情况?

我的datGrid

<DataGrid x:Name="gridSpecifications" AutoGenerateColumns="False" ItemsSource="{Binding MyEntityList}">
    <DataGrid.Resources>
        <DataTemplate DataType="{x:Type DB:CoreNamedEntity}">
            <TextBlock Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </DataGrid.Resources>

    <DataGrid.Columns>
        <DataGridTextColumn Header="Id" Width="auto" Binding="{Binding Path=Id}"></DataGridTextColumn>
        <DataGridTemplateColumn Header="Description">
            <DataGridTemplateColumn.CellTemplateSelector>
                <TS:PhotometricTypeSelector
                    DataTemplateOne="{StaticResource PhantomNameTemplate}" 
                    DataTemplateTwo="{StaticResource PhantomCountTemplate}">
                </TS:PhotometricTypeSelector>
            </DataGridTemplateColumn.CellTemplateSelector>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Hidden">
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <CheckBox  IsChecked="{Binding Hidden}"  Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=DataContext.HideCommand}" CommandParameter="{Binding}"></CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox  IsChecked="{Binding Hidden}" IsEnabled="False"></CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>

        </DataGridTemplateColumn>


    </DataGrid.Columns>
</DataGrid>

这是命令代码的一部分

private void HideCommandExecute(object param)
{
    InputsAccessor inputsAccessor = new InputsAccessor();
    var type = param.GetType();
    int id;
    string name = type.Name;
    var ParamId = type.GetProperty("Id").GetValue(param, null);
    bool ParamHidden = (bool)type.GetProperty("Hidden").GetValue(param, null);
    id = (int)ParamId;
    ....
}

1 个答案:

答案 0 :(得分:0)

IsChecked属性必须具有UpdateSourceTrigger = PropertyChanged,否则在调用该命令之前不会设置它。

     <CheckBox IsChecked="{Binding Hidden, UpdateSourceTrigger=PropertyChanged}"
     CheckBox.Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=DataContext.HideCommand}" CommandParameter="{Binding}"></CheckBox>