Binding to ElementName in DataTemplate

I have one project where I need to validate a TextBox for a correct Text,Number or Date. I am using Validation rules in order to validate data. There is an other TextBlock where error message is shown. So I have a ContentControl which uses ContentTemplateSelector to select a DataTemplate based on data type (Text,Number or Date).

 <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}" />

On the UserControl I have ContentControl with error TextBlock and a button for Accepting value:

<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>

Validations are working OK.

The problem is in Binding to TextBox in the DataTemplate. I am getting binding errors:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=TxtCriteriaValue'. BindingExpression:Path=(0)[0].ErrorContent; DataItem=null; target element is 'TextBlock' (Name='ValidationErrorTextBlock'); target property is 'Text' (type 'String') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=TxtCriteriaValue'. BindingExpression:Path=(Validation.HasError); DataItem=null; target element is 'TextBlock' (Name='ValidationErrorTextBlock'); target property is 'NoTarget' (type 'Object') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=TxtCriteriaValue'. BindingExpression:Path=(Validation.HasError); DataItem=null; target element is 'Button' (Name='AcceptButton'); target property is 'NoTarget' (type 'Object')

How can I bind to control in the DataTemplate?

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

上一篇: 如何在WPF中绑定反布尔属性?

下一篇: 在DataTemplate中绑定到ElementName