有人能告诉我如何绑定到Usercontrol的元素吗?
我尝试使用与IDataErrorInfo一起使用的数据触发器来禁用/启用按钮。
因此,当元素位于同一视图
时,我通常会这样做 <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)上的控件的条件。
我希望像这样轻松访问usercontrol的元素
<Condition Binding="{Binding ElementName=AddressControl , Path=StreetTextBox.(Validation.HasError)}" Value="false" />
但无济于事。任何帮助将不胜感激
答案 0 :(得分:4)
FindAncestor
应该正确解析属性
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type AddressControl}}, Path=StreetTextBox.(Validation.HasError)}" Value="false" />
通过可视树搜索类型AddressControl
,然后解析该属性。