[英]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.