Bind to element within Usercontrol from other Xaml file

你。 提交于 2019-12-10 17:54:30

问题


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:


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.



来源:https://stackoverflow.com/questions/13789933/bind-to-element-within-usercontrol-from-other-xaml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!