在WPF中绑定自定义UserControl的内部元素名称

时间:2015-01-30 09:29:47

标签: wpf xaml binding

我在另一个内部有一个自定义UserControl:

<Grid ....>
    <Contentcontrol Name="ContentPresenter" />
    <Button Content="NextButton" />
</Grid>

我将ContentPresentr内容设置为具有一些TextBox的自定义用户控件。我希望启用/禁用第一个按钮,具体取决于第二个用户控件中的TextBox是否具有任何值。

这是我的尝试:

<UserControl ....>
    <Contentcontrol Name="ContentPresenter" />
    <Button Content="NextButton" >
     <Button.Triggers>
            <MultiDataTrigger>
               <MultiDataTrigger.Conditions>
                 <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type view:UsersUcAddStep0}}, ElementName=TextBoxName}" Value="{x:Null}" />
                </MultiDataTrigger.Conditions>
            </MultiDataTrigger>
        </Button.Triggers>
    </Button>
</UserControl>

ContentControl.xaml(总结,太大了):

<UserControl x:Class="View.UsersUcAddStep0"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Name="AddUserInfo" 
         Loaded="UsersUcAddStep0_OnLoaded"
         Unloaded="OnUnloaded">
<Grid VerticalAlignment="Center" >
    <TextBox Grid.Column="3" Grid.Row="0" Name="TextBoxName" 
                     Margin="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight, 
                     Converter={StaticResource ArithmeticConverter}, ConverterParameter=Int32.Parse(values[0]) / 2.5}"
                     Style="{DynamicResource InnerTextBox}" 
                     Validation.ErrorTemplate="{StaticResource ValidationErrorTemplate}" 
                     LostFocus="TextBox_OnLostFocus"
                     PreviewKeyDown="TextBox_OnPreviewKeyDown"
                     model:TextBoxBehavior.SelectAllTextOnFocus="True">
                <TextBox.Text>
                    <Binding Path="Name" Source="{StaticResource UserInformation}" UpdateSourceTrigger="LostFocus" >
                        <Binding.ValidationRules>
                            <DataErrorValidationRule ValidatesOnTargetUpdated="False" />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
    </Grid>
</UserControl>

但它根本没有编译。有人知道如何进行这种绑定吗?

0 个答案:

没有答案