将焦点设置在 ControlTemplate 中的控件上(第 2 部分)
我对最常见的 WPF 要求之一感到困惑。我已阅读这个问题 但我的解决方案的实现不起作用。
这是外观控件的标记:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest">
<Style TargetType="{x:Type local:CustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl}">
<Border>
<TextBox x:Name="myTextBox" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused"
Value="True">
<Setter Property="FocusManager.FocusedElement"
Value="{Binding ElementName=myTextBox}" />
<Setter TargetName="myTextBox"
Property="Background"
Value="Green" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
这是包含 CustomControl 实例的 Window 的标记:
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
Title="Window1" Height="300" Width="300">
<local:CustomControl x:Name="CCtl" />
</Window>
这是隐藏代码:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Loaded += (RoutedEventHandler)delegate { CCtl.Focus(); };
}
}
加载 Window1 时,文本框变为绿色(表明触发器工作),但焦点仍保留在CCtl 而不是文本框。毫无疑问,这与显示以下数据错误的输出有关:
找不到引用“ElementName=myTextBox”的绑定源。 BindingExpression:(无路径);数据项=空;目标元素是 '自定义控件'(名称='CCtl');目标属性是“FocusedElement” (输入“IInputElement”)。
我不知道为什么会出现这个错误。任何指点都感激不尽,谢谢。
I'm stumped on what must surely be one of the most common WPF requirements. I've read this question but my implementation of the solution does not work.
Here's the markup for the lookless control:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest">
<Style TargetType="{x:Type local:CustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl}">
<Border>
<TextBox x:Name="myTextBox" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused"
Value="True">
<Setter Property="FocusManager.FocusedElement"
Value="{Binding ElementName=myTextBox}" />
<Setter TargetName="myTextBox"
Property="Background"
Value="Green" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Here's the markup for the Window that contains an instance of the CustomControl:
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
Title="Window1" Height="300" Width="300">
<local:CustomControl x:Name="CCtl" />
</Window>
And here's the code-behind:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Loaded += (RoutedEventHandler)delegate { CCtl.Focus(); };
}
}
When Window1 is loaded, the text box turns green (indicating that the trigger works) but focus remains with CCtl and not the text box. Doubtless this has to do with the output displaying the following data error:
Cannot find source for binding with reference 'ElementName=myTextBox'.
BindingExpression:(no path); DataItem=null; target element is
'CustomControl' (Name='CCtl'); target property is 'FocusedElement'
(type 'IInputElement').
I've no idea why this error is appearing. Any pointers gratefully received, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将其用于触发器:
错误告诉您它无法找到 myTextBox,因为该名称不在应用 FocusedElement 属性的范围内。在本例中,它位于 CCtl 实例本身上,无法看到其自己的模板内部。通过在模板内的某些内容上设置属性,绑定可以定位指定的元素。
Try using this for your Trigger instead:
The error is telling you that it can't locate myTextBox because the name isn't in scope where the FocusedElement property is being applied. In this case that's on the CCtl instance itself, which can't see inside its own template. By setting the property on something inside the template the Binding can locate the named element.
我可能是错的,但我认为你的问题在于你的财产触发器。
通过将
TextBox
设置为聚焦,您实际上会使模板化父级上的Trigger
无效,因此触发器会展开并反转将焦点设置在 TextBox 上(因此取消聚焦)它)。I may be wrong, but I think your problem is with your property trigger.
By setting your
TextBox
to being focussed, you in effect invalidate theTrigger
on the Templated Parent, so the trigger unwinds and reverses setting the focus on the TextBox (therefore un-focussing it).