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

How to make style of DoubleUpDown validation like default?

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

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

我通过创建 PositiveNumberToColorConverter 并应用它来做到这一点 border:

 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>

但是,它看起来像这样:

如何在输入负数时高亮显示为第一张图片(在控件中默认输入文本是如何实现的)?

如果您不允许用户输入负值,您应该将 Minimum 属性 设置为 0:

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

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