开发者

Styling a TextBox with IDataErrorInfo in WPF

开发者 https://www.devze.com 2023-01-09 06:46 出处:网络
I\'m trying to change style a text box. By now, I have made that my textbox shows an asterisc on the right of its border with this code:

I'm trying to change style a text box. By now, I have made that my textbox shows an asterisc on the right of its border with this code:

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

But I'd like my textbox be shown with a red triangle on the right upper corner. How could I get this style on my textbox?

Thanks.


I have already done what I wanted, it was just like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消