本文介绍了wpf RowDetailsTemplate焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前拥有一个带有rowdetailstemplate的datagrid,它包含另一个datagrid来显示父对象与子关系。第二个网格有一个列,其中包含一个按钮,当单击时显示另一个对话框。



首次显示一行的详细信息时,用户必须单击一次子网格获取焦点/激活它,然后再次单击以触发按钮单击事件。这只会在第一次显示一行。



这就像网格吞噬的第一次点击。
我已经尝试捕获RowDetailsVisibilityChanged事件来尝试并集中按钮,但仍然似乎没有解决问题。



任何想法? p>

解决方案

我会回答自己的评论,也可能帮助别人。
以下MSDN条目解释并解决了以下问题:



问题是,始终显示的行详细信息需要先获得焦点。
为了规避这个问题,需要一个datagrid预览处理程序:

 < DataGrid.RowStyle> 
< Style TargetType ={x:Type DataGridRow}BasedOn ={StaticResource {x:Type DataGridRow}}>
< EventSetter Event =PreviewMouseLeftButtonDownHandler =SelectRowDetails/>
< / Style>
< /DataGrid.RowStyle>

注意:我已经扩展它,因为它销毁了我的自定义DataGridRow样式,以继承当前使用的。



处理程序本身是

  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);
}
}

它将重点放在行详细信息的内容上,这解决了点击两次的问题。



注意:这些都是从MSDN线程中获取的,这不是我自己的解决方案。


I currently have a datagrid with a rowdetailstemplate which contains another datagrid to show a parent to child relationship. The second grid has a column which contains a button which when clicked displays another dialog.

The first time the details for a row are displayed, the user has to click once in the child grid to gain focus/activate it and then click again to fire the button click event. This only happens the first time a row is shown.

It is like the first click is swallowed by the grid.I have tried capturing the RowDetailsVisibilityChanged event to try and focus the button but it still doesn't seem to have solved the issue.

Any ideas?

解决方案

I'll answer my own comment and it probably helps others too.The following MSDN entry explains and solves the issue:http://social.msdn.microsoft.com/Forums/vstudio/en-US/2cde5655-4b8d-4a12-8365-bb0e4a93546f/activating-input-controls-inside-datagrids-rowdetailstemplate-with-single-click?forum=wpf

The problem is that a row details that is always shown requires gaining the focus first.To circumvent that problem a datagrid preview handler is required:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}"  BasedOn="{StaticResource {x:Type DataGridRow}}">
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="SelectRowDetails"/>
    </Style>
</DataGrid.RowStyle>

Note: I've extended it as it destroyed my custom DataGridRow Style to inherit the currently used one.

The handler itself is

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);
    }
}

It sets the focus to the content of the row details, which solves the click-twice issue.

Note: This all taken from the MSDN thread, it is not my own solution.

这篇关于wpf RowDetailsTemplate焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 21:45