gpt4 book ai didi

c# - DataGrid RowDetails 中的按钮需要两次点击,解决方法会中断双击

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:57 26 4
gpt4 key购买 nike

我有一个带有在 RowDetailsTemplate 中定义的按钮的 DataGrid。问题是单击按钮时,第一次单击被 DataGrid 消耗以选择行,因此需要单击按钮两次。

我已经尝试过此处提供的解决方法:wpf RowDetailsTemplate focus :

XAML:

<DataGrid ItemsSource="{Binding SomeCollection}">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="SelectRowDetails"/>
<Setter Property="DetailsVisibility" Value="{Binding HasCanadet, Converter={StaticResource BoolToVis}}"/>Mode=OneWay}"/>
</Style>
</DataGrid.Resources>
...
<DataGrid.RowDetailsTemplate>
<DataTemplate>
...
<Button Command="{Binding SomeCommand}" ... \>
...
</DataTemplate>
</DataGrid.RowDetailsTemplate>
...
<DataGrid>

代码隐藏:

private void SelectRowDetails(object sender, MouseButtonEventArgs e)
{
var row = sender as DataGridRow;
if (row == null)
{
return;
}
row.Focusable = true;
row.Focus();

var focusDirection = FocusNavigationDirection.Next;
var request = new TraversalRequest(focusDirection);
var elementWithFocus = Keyboard.FocusedElement as UIElement;
if (elementWithFocus != null)
{
elementWithFocus.MoveFocus(request);
}
}

这很好用,但我现在需要检测 DataGrid 行上的双击事件。为此,我将 InputBindings 添加到 DataGrid 并将 SelectedItem 作为命令参数传递:

...
<DataGrid.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding AnotherCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem}" />
</DataGrid.InputBindings>
...

问题是双击仅在 RowDetails 上检测到(这很好),但在双击行本身时检测不到(这不是)。

有什么想法吗?谢谢。

最佳答案

检查事件源是否为DataGridDetailsPresenter

类型
private void SelectRowDetails(object sender, MouseButtonEventArgs e)
{
if(e.Source is DataGridDetailsPresenter) // Like this
{
var row = sender as DataGridRow;
if (row == null)
{
return;
}
row.Focusable = true;
row.Focus();

var elementWithFocus = Keyboard.FocusedElement as UIElement;
if (elementWithFocus != null)
{
elementWithFocus.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
}

关于c# - DataGrid RowDetails 中的按钮需要两次点击,解决方法会中断双击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35182396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com