如何在WPF中的Validation.ErrorTemplate内部进行DataTrigger绑定

时间:2017-12-12 07:52:18

标签: c# wpf validation

我有CustomControlControlTemplate包围。 CustomControl包含另一个CustomControl LabelTextBoxValidation.ErrorTemplate TextBox

我要做的是,当我将CustomControl悬停或焦点TextBox时,应弹出Validation.ErrorTemplate

现在它只会弹出Validation.ErrorTemplate当且仅当我将Border悬停在TextBox周围时。说实话,我不确定它是Border还是StackPanel中的AdornedElementPlaceholder。我真的不知道如何将DataTrigger内的Validation.ErrorTemplate绑定到外Border IsMouseOver属性或IsFocused来自TextBox属性。

到目前为止我的代码下面:

<ControlTemplate TargetType="{x:Type my:CustomControl}" x:Key="CustomTemplate">
<Border BorderBrush="Green" BorderThickness="1" x:Name="outerBorder">
    <my:LabelControl Label="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ViewModel.Label}">
        <TextBox HorizontalAlignment="Stretch" Padding="3 0 3 0" Foreground="{DynamicResource Control.Foreground}">
            <TextBox.Resources>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="CornerRadius" Value="3"/>
                </Style>
            </TextBox.Resources>
            <TextBox.Text>
                <Binding Path="ViewModel.Value" UpdateSourceTrigger="PropertyChanged" RelativeSource="{RelativeSource TemplatedParent}" ValidatesOnDataErrors="True"/>
            </TextBox.Text>
            <TextBox.Style>
                <Triggers>
                    ...
                </Triggers>
            </TextBox.Style>
            <Validation.ErrorTemplate>
                <ControlTemplate>
                    <StackPanel x:Name="BorderBorder">
                        <Border BorderThickness="1" BorderBrush="Red" CornerRadius="3" HorizontalAlignment="Left" >
                            <AdornedElementPlaceholder x:Name="textBox"/>
                        </Border>
                        <Border Background="LightGoldenrodYellow"  CornerRadius="3">
                            <TextBlock Text="{Binding [0].ErrorContent}"/>
                            <Border.Style>
                                <Style>
                                    <Setter Property="Border.Visibility" Value="Collapsed"></Setter>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding ElementName=BorderBorder, Path=IsMouseOver}" Value="true">
                                            <Setter  Property="Border.Visibility" Value="Visible"></Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Border.Style>
                        </Border>
                    </StackPanel>
                </ControlTemplate>
            </Validation.ErrorTemplate>
        </TextBox>
    </my:LabelControl>
</Border>
</ControlTemplate>

因此DataTrigger内的Validation.ErrorTemplate应绑定到ElementName=outerBorder,如果可能的话。{/ p>

我想问题是我在ControlTemplate内有一个ControlTemplate,因此内部DataTrigger Binding ControlTemplate Validation.ErrorTemplate Properties )不了解外部的padding='SAME'

1 个答案:

答案 0 :(得分:1)

您可以将Tag的{​​{1}}属性绑定到控件本身的TextBox属性,然后将IsMouseOver绑定到DataTrigger属性装饰元素:

Tag