如何使DoubleUpDown验证的样式像默认一样?

时间:2017-07-17 08:02:08

标签: c# wpf wpf-controls wpftoolkit wpf-style

当我将文本输入DoubleUpDown控件时,它会被重置并且控件以红色突出显示。 它看起来像这样:

UpDown control

如何将相同的样式应用于负数?

我是通过创建PositiveNumberToColorConverter并将其应用于魔法边框来实现的:

 public class PositiveNumberToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(value is int?)
        {
            var intValue = (int?)value;
            if (intValue == null)
                return Brushes.Transparent;
            else if (intValue.HasValue&&intValue.Value>=0)
                return Brushes.Transparent;

            else if(intValue.HasValue==false)
                return Brushes.Transparent;
            return Brushes.Red;
        }        
        return Brushes.Red;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}


 <Border
                                    Grid.Column="0"                                                                              
                                    BorderThickness="1"
                                    Background="Transparent"                                        
                                    BorderBrush="{Binding ObjectViewModel.Value,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource positiveNumberConverter}}"                                                                                                                     
                            IsEnabled="{Binding ObjectViewModel.IsEnabled,Mode=TwoWay,TargetNullValue=False,FallbackValue=False,UpdateSourceTrigger=PropertyChanged}"
                                    >
                            <xcd:DoubleUpDown
                          HorizontalAlignment="Stretch"
                          Margin="5"
                          Grid.Column="0"
                          Minimum="0"
                          Height="{Binding ElementName=tbName,Path=Height}"
                          Width="Auto"
                          IsEnabled="{Binding ObjectViewModel.IsEnabled,Mode=TwoWay,TargetNullValue=False,FallbackValue=False,UpdateSourceTrigger=PropertyChanged}"
                          Text="{Binding ObjectViewModel.Value,Mode=TwoWay,UpdateSourceTrigger=LostFocus}" >
                                <xcd:DoubleUpDown.Resources>
                                    <Style TargetType="Popup">
                                        <Setter Property="TextBlock.TextAlignment" Value="Left"/>
                                    </Style>
                                </xcd:DoubleUpDown.Resources>
                            </xcd:DoubleUpDown>
                        </Border>

但是,它看起来像这样:

With Border

如何在输入负数时将其突出显示为第一张图片(默认情况下如何在文本输入中实现控制)?

1 个答案:

答案 0 :(得分:1)

如果您不让用户输入负值,则应将Minimum属性设置为0

<xctk:DoubleUpDown ... Minimum="0" />

您看到的红色边框是默认Validation.ErrorTemplate的一部分。