[英]Make a binding on ElementName to change when trigered
I'm probably missing something because what I do is suposed to work I think. 我可能错过了一些东西,因为我的工作应该可以正常工作。 I'm trying to bind the ElementName of a value to change when trigered.
我试图绑定值的ElementName,以在触发时更改。
here's the code (it is in the style): 这是代码(采用样式):
<ContentPresenter>
<ContentPresenter.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Tick}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName={Binding FieldNameFocus}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
And get this error: 并得到这个错误:
Error 2 A 'Binding' cannot be set on the 'ElementName' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject. C:\Users\xavier\Documents\Visual Studio 2012\Projects\Test_Validation\Test_Validation\MainWindow.xaml 1 2 Test_Validation
Maybe I'm not in the right path though... I just want to have focusedElement to change according to FieldNameFocus
when Tick
becomes True. 也许我走的路不对……我只想让
Tick
变为True时,根据FieldNameFocus
更改focusElement。 (Both are in my dataContext) (两者都在我的dataContext中)
Thanks in advance. 提前致谢。
Your binding should look like this: Value="{Binding ElementName=YourElementName, Path=PropertyOfTheElement}"
您的绑定应如下所示:
Value="{Binding ElementName=YourElementName, Path=PropertyOfTheElement}"
Example binding by ElementName: 通过ElementName绑定的示例:
<TextBox Name="tbx1" Text="TextBox"/>
<TextBox Name="tbx2" Text="{Binding ElementName=tbx1, Path=Text}"/>
Hello the error is pretty clear, you cannot use Binding on with ElementName. 您好,错误非常清楚,您不能将Binding与ElementName一起使用。 When using ElementName you should provide the name of a control present in your xaml File as the previous post showed.
使用ElementName时,应提供xaml文件中存在的控件的名称,如前一篇文章所示。 As your trigger is bind to a boolean value you might want to replace
"ElementName={Binding FieldNameFocus}"
by "ElementName=controlNameOnWhichYouWantToSetFocus"
. 当您的触发器绑定到布尔值时,您可能希望将
"ElementName={Binding FieldNameFocus}"
替换为"ElementName=controlNameOnWhichYouWantToSetFocus"
。
Eg <Textbox x:Name="ControlIWantFocusOnWhenTickIstrue">
then in your trigger 例如
<Textbox x:Name="ControlIWantFocusOnWhenTickIstrue">
然后在您的触发器中
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName="ControlIWantFocusOnWhenTickIstrue"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.