[英]Make a binding on ElementName to change when trigered
我可能错过了一些东西,因为我的工作应该可以正常工作。 我试图绑定值的ElementName,以在触发时更改。
这是代码(采用样式):
<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>
并得到这个错误:
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
也许我走的路不对……我只想让Tick
变为True时,根据FieldNameFocus
更改focusElement。 (两者都在我的dataContext中)
提前致谢。
您的绑定应如下所示: Value="{Binding ElementName=YourElementName, Path=PropertyOfTheElement}"
通过ElementName绑定的示例:
<TextBox Name="tbx1" Text="TextBox"/>
<TextBox Name="tbx2" Text="{Binding ElementName=tbx1, Path=Text}"/>
您好,错误非常清楚,您不能将Binding与ElementName一起使用。 使用ElementName时,应提供xaml文件中存在的控件的名称,如前一篇文章所示。 当您的触发器绑定到布尔值时,您可能希望将"ElementName={Binding FieldNameFocus}"
替换为"ElementName=controlNameOnWhichYouWantToSetFocus"
。
例如<Textbox x:Name="ControlIWantFocusOnWhenTickIstrue">
然后在您的触发器中
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName="ControlIWantFocusOnWhenTickIstrue"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.