Search code examples
c#wpfwpf-controlswpftoolkitwpf-style

How to make style of DoubleUpDown validation like default?


When I enter the text into the DoubleUpDown control, it is reset and the control is highlighted in red. It looks like this:

UpDown control

How can I apply the same style to a negative number?

I do it by creating PositiveNumberToColorConverter and apply it witch 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>

But, it looks like this:

With Border

How to make so that at input of a negative number it was highlighted as on the first picture (how it is implemented in control by default for text entry)?


Solution

  • If you don't the user to be able to input negative values, you should set the Minimum property to 0:

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

    The red border that you are seeing is part of the default Validation.ErrorTemplate.