数据网格中的 WPF 弹出焦点 [英] WPF Popup focus in data grid

查看:15
本文介绍了数据网格中的 WPF 弹出焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建要在 DataGrid 编辑模板中使用的自定义 UserControl.它看起来像这样:

I'm creating a custom UserControl to be used inside a DataGrid editing template. It looks like this:

<UserControl
   x:Class="HR.Controls.UserPicker"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid>
        <TextBlock x:Name="PART_TextBox" Text="Hello WOrld" />
        <Popup Width="234" Height="175" IsOpen="True" StaysOpen="True"

             Placement="Bottom"
             PlacementTarget="{Binding ElementName=PART_TextBox}"
         >
            <TextBox
                  x:Name="searchTextBox"
                  Text="&gt;Enter Name&lt;"/>
        </Popup>
    </Grid>
</UserControl>

我已经缩小了一些代码.看来,如果我直接在 CellEditingTemplate 中放置一个带有文本框的弹出窗口,则文本框获得焦点没问题.当我将该代码移动到 UserControl 时,我无法在编辑单元格时再选择文本框.

edit: I've narrowed down the code a bit. It seems that if I put a Popup with textbox inside the CellEditingTemplate directly the textbox gets focus no problem. When I move that code into a UserControl I can no longer select the textbox when editing the cell.

UserControl 是否对焦点做了一些有趣的事情?

Is the UserControl doing something funny with the focus ?

问题是当我编辑数据网格中的单元格时,我显示用户控件,但我无法在 TextBox 搜索文本框中单击.当我点击它时,弹出窗口关闭,单元格恢复为默认值.

The problem is when i edit the cell in the datagrid I get the user control showing up but I can't click in the TextBox searchTextBox. When I click on it the popup closes and the cell goes back to default.

我尝试复制并粘贴用户控件内的所有代码,然后将其直接粘贴到 CellEditingTemplate 中,并以应有的方式进行交互.

I have tried copying and pasting all the code inside the user control and pasting it directly into the CellEditingTemplate and that interacts the way it should.

我只是想知道 UserControl 是否做了一些奇怪的事情来阻止弹出窗口获得焦点,因为它直接放置在 CellEditingTemplate 中时按预期工作?

I was just wondering if the UserControl did something weird that prevents a popup from gaining focus because it works as expected when directly placed in the CellEditingTemplate ?

谢谢,劳尔

推荐答案

Set FocusManager.IsFocusScope 将弹出窗口的附加属性设为 True

Set FocusManager.IsFocusScope Attached Property on the Popup to True

这篇关于数据网格中的 WPF 弹出焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆