0

我对应用程序中的所有组合框都有全局样式。对于错误验证,我使用 IDataErrorInfo。因此,当出现错误时,我希望所有文本框都具有这样的自定义视图: 在此处输入图像描述具有不同的 ErrorContent(鼠标应该在圆圈上以显示带有错误信息的工具提示)。我的组合框样式是:

<Style x:Key="AlStyleTextBoxArial12" TargetType="{x:Type TextBox}">
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="12pt"/>
    <Setter Property="Margin" Value="3,3,3,3"/>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="True">
                            <Grid DockPanel.Dock="Right" Margin="-23,0,0,0" Width="15" Height="15">
                                <Grid.ToolTip>
                                   <ToolTip Content="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                                </Grid.ToolTip>
                                <Ellipse Fill="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                                <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="11pt" FontWeight="Bold" Margin="0,0,0,1"/>
                            </Grid>
                            <Border BorderBrush="Red" BorderThickness="1">
                                <AdornedElementPlaceholder/>
                            </Border>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

我的 TextBox 是这样实现的:

 <TextBox Text="{Binding Snr, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                                             Grid.Row="3" Grid.Column="2"/>

当我出现错误时,TextBox 的视图是可以的,但我无法设置工具提示。它总是空的。任何想法为什么我无法获得 ErrorContent?

4

1 回答 1

0

我已经改变了我的风格:

 <Style x:Key="AlStyleTextBoxArial12" TargetType="{x:Type TextBox}">
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="12pt"/>
    <Setter Property="Margin" Value="3,3,3,3"/>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="True">
                            <Grid DockPanel.Dock="Right" Margin="-23,0,0,0" Width="15" Height="15">
                                <Grid.ToolTip>
                                    <ToolTip Content="{Binding [0].ErrorContent}"/>
                                </Grid.ToolTip>
                                <Ellipse Fill="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                                <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="11pt" FontWeight="Bold" Margin="0,0,0,1"/>
                            </Grid>
                            <Border BorderBrush="Red" BorderThickness="1">
                                <AdornedElementPlaceholder x:Name="customAdorner" ToolTip="{Binding [0].ErrorContent}"/>
                            </Border>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

它现在正在工作。

于 2015-05-19T13:26:42.353 回答