Bind to element within Usercontrol from other Xaml

2019-07-14 05:51发布

Could anyone tell me how i can bind to a Element of a Usercontrol?

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>

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

1条回答
再贱就再见
2楼-- · 2019-07-14 06:15

The FindAncestor should resolve the property correctly

  <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type AddressControl}}, Path=StreetTextBox.(Validation.HasError)}" Value="false"  />

This bacially searches back though the visual tree looking for type AddressControl and resolves the property.

查看更多
登录 后发表回答