简体   繁体   English

WPF 焦点 DataGrid 在 GotFocus 事件期间的另一个控件问题

[英]WPF focus DataGrid during GotFocus event of another control problem

According to the asnwer to Event for when KeyboardNavigation.TabNavigation Cycle occurs , the go-to solution is to add invisible control as last TabIndex of a Detail focus scope, handling GotFocus() on this dummy element.根据对事件何时发生 KeyboardNavigation.TabNavigation Cycle的回答,首选解决方案是添加不可见控件作为细节焦点 scope 的最后一个 TabIndex,处理此虚拟元素上的GotFocus() As part of handling this 'event' I would like to move focus back to master grid MasterDG.Focus() :作为处理此“事件”的一部分,我想将焦点移回主网格MasterDG.Focus()

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <DataGrid Name="MasterDG" ItemsSource="{Binding Items}" FocusManager.IsFocusScope="True"/>
    <StackPanel Name="Detail" Grid.Row="1"  FocusManager.IsFocusScope="True">
        <TextBox/>
        <TextBox/>
        <TextBox/>
        <Control Name="DummyControl" 
                 GotFocus="DummyControl_GotFocus"/>
    </StackPanel>
</Grid>

Event handler事件处理程序

private void DummyControl_GotFocus(object sender, RoutedEventArgs e)
{
    Save(); //save when done editing last element of detail
    MasterDG.Focus();
}

However this causes not only MasterDG to be focused but also enter Edit mode on current cell and insert \t character overwriting any cell content.然而,这不仅会导致MasterDG被聚焦,还会在当前单元格上进入编辑模式插入\t字符以覆盖任何单元格内容。 How can I fix the issue?我该如何解决这个问题?
Note the actual contents of Detail are dynamically generated.注意 Detail 的实际内容是动态生成的。

An easy workaround would be to call Focus() in the next dispatcher cycle:一个简单的解决方法是在下一个调度程序周期中调用Focus()

private void DummyControl_GotFocus(object sender, RoutedEventArgs e)
{
    Save(); //save when done editing last element of detail
    Dispatcher.BeginInvoke(new Action(() => MasterDG.Focus()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM