0

行ヘッダーでチェックボックスを使用するWPFアプリビューにDatagridがあります。

<DataGrid.RowHeaderTemplate>
<DataTemplate>
    <Grid >
        <CheckBox BorderThickness="0"  
                  Command="{Binding DataContext.AssignPartsToGroupCommand, RelativeSource={RelativeSource FindAncestor,
                                                                               AncestorType={x:Type UserControl}}}"                                          
                                    >
            <CheckBox.CommandParameter>
                <MultiBinding Converter="{StaticResource PartsGroupAssignConverter}">
                    <Binding Path="IsChecked" RelativeSource="{RelativeSource Self}" Mode="OneWay"/>
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, 
                                                             AncestorType={x:Type DataGridRow}}" 
                             Path="DataContext" Mode="OneWay"/>
                </MultiBinding>
            </CheckBox.CommandParameter>
            <CheckBox.IsChecked>
                <MultiBinding  Converter="{StaticResource PartsGroupAssignedConverter}"  Mode="OneWay">
                    <Binding ElementName="partsGroupGrid" Path="SelectedItem.id"></Binding>
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, 
                                                             AncestorType={x:Type DataGridRow}}" 
                             Path="DataContext" Mode="OneWay"/>
                    <Binding Path="IsSelected" Mode="OneWay"
                             RelativeSource="{RelativeSource FindAncestor,
                              AncestorType={x:Type DataGridRow}}"
                             />
                </MultiBinding>
            </CheckBox.IsChecked>
        </CheckBox>
    </Grid>
</DataTemplate>

ご覧のとおり、CheckBox プロパティ "IsSelected" を複数の値にバインドし、そのうちの 1 つが DataGrid の行選択です。

   <Binding Path="IsSelected" 
            Mode="OneWay"
            RelativeSource="{RelativeSource FindAncestor,
                                  AncestorType={x:Type DataGridRow}}"
                                 />

私の問題は、行を選択してCheckBoxをチェックすると、CheckBoxにリンクされたコマンドがトリガーされないことです。しかし、手動で(マウスを使用して)実行するとトリガーされます。どうすればこれを解決できますか?

4

1 に答える 1

0

CheckBox のソース コードによると、目的のアプローチはサポートされていません。コマンドは、クリックした後にのみ呼び出されます。

ただし、小さな CheckBox の子孫を作成して、必要な動作を実現できます。この子孫は、CheckBox.IsCheckedプロパティの変更を追跡し、コマンドを実行します。

次のように実行できます。

    public class MyCheckBox : CheckBox {
    static MyCheckBox() {
        IsCheckedProperty.OverrideMetadata(typeof(MyCheckBox), new FrameworkPropertyMetadata((o, e) => ((MyCheckBox)o).OnIsCheckedChanged()));
    }

    readonly Locker toggleLocker = new Locker();
    readonly Locker clickLocker = new Locker();

    void OnIsCheckedChanged() {
        if (clickLocker.IsLocked)
            return;
        using (toggleLocker.Lock()) {
            OnClick();
        }
    }

    protected override void OnToggle() {
        if (toggleLocker.IsLocked)
            return;
        base.OnToggle();
    }

    protected override void OnClick() {
        using (clickLocker.Lock()) {
            base.OnClick();
        }
    }
}

そして Locker クラス:

    class Locker : IDisposable {
    int count = 0;
    public bool IsLocked { get { return count != 0; } }
    public IDisposable Lock() {
        count++;
        return this;
    }
    public void Dispose() { count--; }
}
于 2016-09-08T11:01:57.753 に答える