仅使用XAML无法将焦点设置在Popup中的控件上 [英] unable to set focus on a control in Popup by using XAML only
问题描述
嗨
我已经在资源文件中定义了Popup控件的样式,并在UserControl中定义了Popup本身.
现在我想当我的弹出窗口被加载时,焦点应该放在弹出窗口的弹出窗口内容中包含的按钮上.
我只想在XAML中这样做.
以下是我的代码:
Hi
I have defined the style for a Popup control in my resource file and Popup itself in a UserControl.
Now i want that when my pop is Loaded the focus should be on the Button contained in the Popup Content of Popup
I want to do it in XAML only.
Below is my code:
<Style x:Key="myContentStyle" TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Button x:Name="DoneButton"
HorizontalAlignment="Right"
Grid.Row="4"
Width="Auto"
Margin="4,4,4,4"/>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsOpen,RelativeSource={RelativeSource AncestorType=Popup}}" Value="True">
<Setter TargetName="DoneButton" Property="FocusManager.FocusedElement" Value="{Binding ElementName=DoneButton}" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Embedded popup -->
<Popup Style="{DynamicResourcemyPopupStyle}" >
<ContentControlStyle="{DynamicResourcemyContentStyle}"/>
</Popup>
当我按下"Tab"按钮时,焦点仍然位于背景"窗口控件上.
任何帮助!!
when i press the Tab button Focus is still on the Background windows controls.
Any help !!
推荐答案
很抱歉,这是正确的解决方法
首先为边框创建样式:
I am sorry this is the right solution
first create a style for the border:
<style x:key="borderStyle" targettype="{x:Type Border}" xmlns:x="#unknown">
<eventsetter event="Loaded" handler="OnBorderLoaded" />
</style>
然后在后面的代码中创建逻辑:
then create your logic in the code behind:
private void OnBorderLoaded(object sender, RoutedEventArgs e)
{
var border = sender as Border;
if (border != null)
{
border.Child.Focus();
}
}
创建您的弹出样式并使边框采用较早的样式:
the create your popup style and make the border take the earlier style:
<style x:key="myPopupStyle" targettype="{x:Type Popup}" xmlns:x="#unknown">
<setter property="Child">
<setter.value>
<border background="White" style="{StaticResource borderStyle}">
<button name="DoneButton" content="Done Button">
HorizontalAlignment="Right"
Grid.Row="4"
Width="Auto"
Margin="4,4,4,4">
</button>
</border>
</setter.value>
</setter>
</style>
最终使您的弹出窗口具有正确的样式:
finally make your popup and give it the right style:
<popup style="{StaticResource myPopupStyle}" grid.row="1" />
据我所知,无法使用可用的控件来做到这一点.
如果您愿意构建一个简单的控件,我将继承PopUp表单,并捕获OnOpen事件并设置焦点.获取焦点的控件将由依赖项属性设置.
另一种选择是创建一个行为基本相同的行为.在这种情况下,执行此操作的bahavior属性将成为获得焦点的控件.
As far as I know there is no way to do this with available controls.
If you are willing to build a simple control, I would inherit form PopUp, and catch the OnOpen event and set the focus. The control to get focus would be set by a dependency property.
Another option is to create a bahaviour which basically does the same thing. In this case the bahavior property to do this would be the control to get focus.
这篇关于仅使用XAML无法将焦点设置在Popup中的控件上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!