从其他Xaml文件绑定到Usercontrol中的元素 [英] Bind to element within Usercontrol from other Xaml file
问题描述
有人可以告诉我如何绑定到Usercontrol的元素吗?
Could anyone tell me how i can bind to a Element of a Usercontrol?
我正在尝试使用与IDataErrorInfo一起使用的数据触发器来禁用/启用按钮.
Im trying to disable/enable a button using a datatrigger working together with IDataErrorInfo.
所以当元素在同一视图上时,我通常会这样做
so i usually do this like this when the elements are on the same view
<Button Name="AddEditButton" Content="{Binding ButtonContent}" Command="{Binding AddCustomerCommand}" HorizontalAlignment="Center" Margin="0 10" >
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="false" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=CustomerFirstNameTextBox, Path=(Validation.HasError)}" Value="false" />
<Condition Binding="{Binding ElementName=CustomerLastNameTextBox, Path=(Validation.HasError)}" Value="false" />
<Condition Binding="{Binding ElementName=CustomerEmailTextBox, Path=(Validation.HasError)}" Value="false" />
<Condition Binding="{Binding ElementName=CustomerPhoneTextBox, Path=(Validation.HasError)}" Value="false" />
<Condition Binding="{Binding ElementName=CustomerCellphoneTextBox, Path=(Validation.HasError)}" Value="false" />
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="true" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
但是现在我需要添加一个条件,该条件绑定到我的视图正在使用的Usercontrol(AdressControl)上的控件上.
But now i need to add a condition that binds to a control on a Usercontrol (AdressControl) that is being used by my view.
我希望像这样轻松地访问用户控件的元素
I was hoping to easily access the elements of the usercontrol like this
<Condition Binding="{Binding ElementName=AddressControl , Path=StreetTextBox.(Validation.HasError)}" Value="false" />
但无济于事.任何帮助将不胜感激
but to no avail. Any help would be much appreciated
推荐答案
FindAncestor
应该正确解析属性
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type AddressControl}}, Path=StreetTextBox.(Validation.HasError)}" Value="false" />
这会在可视化树中进行搜索,以查找类型AddressControl
并解析该属性.
This bacially searches back though the visual tree looking for type AddressControl
and resolves the property.
这篇关于从其他Xaml文件绑定到Usercontrol中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!