C#WPF如何将文本框周围的红色矩形更改为带有感叹号的红色圆圈

时间:2015-03-12 10:51:07

标签: c# xaml wpf-controls errorprovider

我有TextBox连接到类属性。

<TextBox Name="txtbSleeveLength"   
         Grid.Row="0" 
         Grid.Column="1" 
         VerticalAlignment="Center" 
         HorizontalAlignment="Center" 
         Height="23" Margin="1" 
         Text="{Binding Path=SleeveLength, StringFormat=N2}" 
         Width="120" 
         TextAlignment ="Center" 
         GotFocus="txtbSleeveLength_GotFocus" />

来自班级的属性

public class SleevePattern : Shape, INotifyPropertyChanged
{
 //...
    public event PropertyChangedEventHandler PropertyChanged;
    //...
    protected void Notify(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    //...
  public double SleeveLength
    {

        get { return sleeveLength; }
        set
        {
            if (value != sleeveLength)
            {
                sleeveLength = value;
                Notify("SleeveLength");
            }

        }
    }
    //...    

}

由于该属性不可为空,因此有两种效果。一开始TextBox中有“0.00”,如果TextBox未满足,则文本框周围会出现一个红色矩形。

现在提问  TextBox属性负责此矩形(如果存在)?我听说有ErrorProvider。可以在XAML标签中使用它来获取异常并将矩形更改为带有感叹号的红色圆圈吗?

1 个答案:

答案 0 :(得分:0)

解决方法可以是使用边框,也可以使用样式。

    <Border BorderBrush="Red" >
            <Border.Style>
                <Setter Property="BorderThickness" Value="0"/>
                <Style TargetType="Border">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding SleeveLength}" Value="0">
                            <Setter Property="BorderThickness" Value="2"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <TextBox ...></TextBox>
        </Border>