我目前有一个带有rowdetailstemplate的数据网格,它包含另一个数据网格来显示父子关系。第二个网格有一个列,其中包含一个按钮,当单击该按钮时,将显示另一个对话框。
第一次显示行的详细信息时,用户必须在子网格中单击一次以获得焦点/激活它,然后再次单击以触发按钮单击事件。这只会在第一次显示一行时发生。
这就像第一次点击被网格吞没一样。我已经尝试捕获RowDetailsVisibilityChanged事件,以尝试并聚焦按钮,但似乎仍然没有解决问题。
有什么想法吗?
发布于 2014-02-05 02:14:51
我会回答我自己的评论,这可能也会对其他人有所帮助。下面的MSDN条目解释并解决了这个问题: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
问题是,总是显示行详细信息的行需要首先获得焦点。为了绕过这个问题,需要一个数据网格预览处理程序:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="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线程,这不是我自己的解决方案。
发布于 2014-04-28 15:50:58
我找到了一个很好的解决方案:D
我只有一行代码可以解决这个问题,但是有10行代码来描述问题所在。以下是解决方案:
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
// to stop RowDetails from eating the first click.
if (e.Property.Name == "SelectedItem" && CurrentItem == null) CurrentItem = SelectedItem;
}并找到详细的here请。
https://stackoverflow.com/questions/10519593
复制相似问题