1

私はPOSアプリケーションを構築しています.エンドユーザーがデータグリッドのトグル選択モードを使用できるようにしたい.IEは複数の行をクリックでき、クリックされた各アイテムはSelectedItemsプロパティに蓄積されます-行をクリックします行の選択が解除されます。別のスタックオーバーフローの質問でこのコードを見つけました:

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DoCheckRow" />
    </Style>
</DataGrid.Resources>

public void DoCheckRow(object sender, MouseButtonEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;
    if (cell != null && !cell.IsEditing)
    {
        DataGridRow row = VisualHelpers.TryFindParent<DataGridRow>(cell);
        if (row != null)
        {
            row.IsSelected = !row.IsSelected;
            e.Handled = true;
            Debug.WriteLine(sender);
        }
    }
}

これは、トグル選択モードに関する限り、私が望むものを効果的に提供しますが、ボタンをCellTemplateとして追加するとe.Handled = true;、イベントバブルを停止する上記のコードで設定しているため、クリックしてもボタンコマンドは起動されません。両方に対応できる方法はありますか?

4

3 に答える 3

1

対応する行の選択を切り替えるチェックボックスを使用してこれを行うこともできます。

<DataGrid.RowHeaderTemplate>
   <DataTemplate>
      <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
                           RelativeSource={RelativeSource FindAncestor,
                           AncestorType={x:Type DataGridRow}}}"/>
   </DataTemplate>
</DataGrid.RowHeaderTemplate>
于 2016-01-26T18:22:48.100 に答える
1

ボタンに AttachedBehavior を配置してみてはいかがでしょうか。このようにして、状況からコマンドを取り出し、AttachedBehavior でクリック イベントを処理します。

于 2012-08-24T15:24:45.790 に答える
0

いくつかのヘルパー関数を使用して視覚的な子/親を見つけ、いくつかのヒット テストを行うことで解決できました。

public void DoCheckRow(object sender, MouseButtonEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;
    if (cell != null && !cell.IsEditing)
    {
        DataGridRow row = VisualHelpers.TryFindParent<DataGridRow>(cell);
        if (row != null)
        {
            Button button = VisualHelpers.FindVisualChild<Button>(cell, "ViewButton");

            if (button != null)
            {
                HitTestResult result = VisualTreeHelper.HitTest(button, e.GetPosition(cell));

                if (result != null)
                {
                    // execute button and do not select / deselect row
                    button.Command.Execute(row.DataContext);
                    e.Handled = true;
                    return;
                }
            }

            row.IsSelected = !row.IsSelected;
            e.Handled = true;
        }
    }
}

これは最も洗練されたソリューションではありませんが、私が使用している MVVM パターンで動作します。

于 2012-08-24T16:55:51.393 に答える