在WPF中使用IDataErrorInfo设置TextBox样式

时间:2010-07-27 17:54:48

标签: c# wpf textbox idataerrorinfo

我正在尝试更改文本框的样式。到目前为止,我已经使我的文本框在其边框右侧显示了一个带有此代码的星号:

    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <TextBlock DockPanel.Dock="Right" HorizontalAlignment="Right" Foreground="Red" FontSize="14pt" FontWeight="Bold" Text="*"></TextBlock>
                        <Border BorderBrush="Red" BorderThickness="1">
                            <AdornedElementPlaceholder Name="controlWithError"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
            </Style.Triggers>
    </Style>

但是我希望我的文本框在右上角显示一个红色三角形。我怎么能在我的文本框中获得这种风格?

感谢。

1 个答案:

答案 0 :(得分:3)

我已经做了我想做的事,就像这样:

<Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <Polygon Points="0,0 10,0 0,10 0,0" HorizontalAlignment="Right" Fill="Red" FlowDirection="RightToLeft"></Polygon>
                        <Border BorderBrush="Red" BorderThickness="1">
                            <AdornedElementPlaceholder Name="controlWithError" />
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
            </Style.Triggers>
    </Style>