在DataTemplate中绑定到ElementName

我有一个项目需要验证文本框的正确文本,数字或日期。 我正在使用验证规则来验证数据。 有一个其他TextBlock显示错误消息。 所以我有一个ContentControl,它使用ContentTemplateSelector根据数据类型(文本,数字或日期)选择一个DataTemplate。

 <DataTemplate x:Key="TextCriteria">
    <TextBox DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}"
             Text="{Binding Path=SelectedCriteria.DefaultValue, UpdateSourceTrigger=PropertyChanged}"
             Name="TxtCriteriaValue">
    </TextBox>
</DataTemplate>

<DataTemplate x:Key="NumberCriteria">
    <Grid  DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">    
        <TextBox Name="TxtCriteriaValue">
            <TextBox.Text>
                <Binding Path="SelectedCriteria.DefaultValue" UpdateSourceTrigger="PropertyChanged"  StringFormat="D" >
                    <Binding.ValidationRules>
                        <extra1:CriteriaNumberValueValidationRule ValidatesOnTargetUpdated="True"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </Grid>
</DataTemplate>

<DataTemplate x:Key="DateCriteria">
    <Grid  DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">
        <TextBox Name="TxtCriteriaValue">
            <TextBox.Text>
                <Binding Path="SelectedCriteria.DefaultValue" UpdateSourceTrigger="PropertyChanged"  StringFormat="d">
                    <Binding.ValidationRules>
                        <extra1:CriteriaDateValueValidationRule ValidatesOnTargetUpdated="True"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>                
    </Grid>
</DataTemplate>

<extra1:CriteriaTypeTemplateSelector
    x:Key="CriteriaTypeTemplateSelector"
    TextDataTemplate="{StaticResource TextCriteria}"
    NumberDataTemplate="{StaticResource NumberCriteria}"
    DateDataTemplate="{StaticResource DateCriteria}" />

在用户控件我有ContentControl错误TextBlock和一个按钮接受值:

<ContentControl Grid.Row="1"
                Grid.Column="1"
                x:Name="ContentControl"
                ContentTemplateSelector="{StaticResource CriteriaTypeTemplateSelector}"
                Content="{Binding SelectedCriteria.CriteriaValueType}"
                IsTabStop="False"/>
<TextBlock Grid.Row="2" Grid.Column="1"
           x:Name="ValidationErrorTextBlock"
           Text="{Binding ElementName=TxtCriteriaValue, Path=(Validation.Errors)[0].ErrorContent,Converter={StaticResource DebugConverter}}">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition
                            Binding="{Binding (Validation.HasError), Converter={StaticResource ErrorCollectionToVisibility}, ElementName=TxtCriteriaValue}"
                            Value="True"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Visibility" Value="Visible"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
<StackPanel Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right" Orientation="Horizontal">
    <Button Content="Accept"
            IsDefault="True"
            Name="AcceptButton"
            Command="{Binding AcceptSingleValueCommand}">
        <Button.Style>
            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ToolTipBasicButton}">
                <Setter Property="IsEnabled" Value="False"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding (Validation.HasError), ElementName=TxtCriteriaValue}" Value="False">
                        <Setter Property="IsEnabled" Value="True"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
</StackPanel>

验证工作正常。

问题在于绑定到DataTemplate中的TextBox。 我遇到绑定错误:

System.Windows.Data错误:4:找不到与引用'ElementName = TxtCriteriaValue'绑定的源。 BindingExpression:路径=(0)[0] .ErrorContent; 的DataItem = NULL; 目标元素是'TextBlock'(Name ='ValidationErrorTextBlock'); 目标属性是'文本'(类型'字符串')System.Windows.Data错误:4:找不到与引用'ElementName = TxtCriteriaValue'绑定的源。 BindingExpression:路径=(Validation.HasError); 的DataItem = NULL; 目标元素是'TextBlock'(Name ='ValidationErrorTextBlock'); 目标属性是'NoTarget'(输入'Object')System.Windows.Data错误:4:找不到与参考'ElementName = TxtCriteriaValue'绑定的源。 BindingExpression:路径=(Validation.HasError); 的DataItem = NULL; 目标元素是'Button'(Name ='AcceptButton'); 目标属性是'NoTarget'(键入'Object')

我如何绑定到DataTemplate中的控件?

链接地址: http://www.djcxy.com/p/44647.html

上一篇: Binding to ElementName in DataTemplate

下一篇: WPF DataTemplate Textblock binding