2

C# と WPF を使用してアプリを開発しています。独自のスライダー カスタム コントロールがあります。同じウィンドウ上のテキストボックス。私のスライダーのすべてのプロパティはDependencyProperty.

テキストボックスを使用してスライダーのプロパティを変更します。テキストボックスで使用ValidationRuleしたい。独自の ValidationRule (ValidationRule クラスから派生) を作成しました。それにいくつかのパラメーターを渡したいですValidationRule。コードは次のとおりです。

テキストボックス:

<TextBox HorizontalAlignment="Left" Height="24" Margin="10,169,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="40" Style="{DynamicResource archiveSearchTextBox}" MaxLength="3" HorizontalContentAlignment="Center" TabIndex="2">
        <TextBox.Text>
            <Binding UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" ElementName="gammaSlider" Path="LeftThumbValue" NotifyOnValidationError="True" ValidatesOnExceptions="True" ValidatesOnDataErrors="True">
                <Binding.ValidationRules>
                    <ExceptionValidationRule/>
                    <local:ZeroTo255MinMax>
                        <local:ZeroTo255MinMax.Parameters>
                            <local:ValidationParameters NumberCombineTo="{Binding ElementName=gammaSlider, Path=RightThumbValue}" ValTypeFor0to255="ShouldBeSmaller"/>
                        </local:ZeroTo255MinMax.Parameters>
                    </local:ZeroTo255MinMax>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

ZeroTo255MinMax ValidationRule:

 public class ZeroTo255MinMax : ValidationRule
{
    private ValidationParameters _parameters = new ValidationParameters();
    public ValidationParameters Parameters
    {
        get { return _parameters; }
        set { _parameters = value; }
    }

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        string numberStr = value as string;
        int val;

        if (int.TryParse(numberStr, out val))
        {
            if (val < 0 || val > 255)
                return new ValidationResult(false, "");
            else if (Parameters.ValTypeFor0to255 == ValidationParameters.ValTypes.ShouldBeBigger)
            {
                if (val <= Parameters.NumberCombineTo || val - Parameters.NumberCombineTo < 2)
                    return new ValidationResult(false, "");
            }
            else if (Parameters.ValTypeFor0to255 == ValidationParameters.ValTypes.ShouldBeSmaller)
            {
                if (val >= Parameters.NumberCombineTo || Parameters.NumberCombineTo - val < 2)
                    return new ValidationResult(false, "");
            }
            return new ValidationResult(true, "");
        }
        else
            return new ValidationResult(false, "");
    }
}

public class ValidationParameters : DependencyObject
{
    public enum ValTypes { ShouldBeSmaller, ShouldBeBigger };
    public static readonly DependencyProperty NumberCombineToProperty = DependencyProperty.Register("NumberCombineTo", typeof(int), typeof(ValidationParameters), new PropertyMetadata(0, new PropertyChangedCallback(OnNumberCombineToChanged)));
    public static readonly DependencyProperty ValTypeFor0to255Property = DependencyProperty.Register("ValTypeFor0to255", typeof(ValTypes), typeof(ValidationParameters), new PropertyMetadata(ValTypes.ShouldBeBigger));

    private static void OnNumberCombineToChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { d.CoerceValue(NumberCombineToProperty); }

    public int NumberCombineTo
    {
        get { return (int)GetValue(NumberCombineToProperty); }
        set { SetValue(NumberCombineToProperty, value); }
    }

    public ValTypes ValTypeFor0to255
    {
        get { return (ValTypes)GetValue(ValTypeFor0to255Property); }
        set { SetValue(ValTypeFor0to255Property, value); }
    }
}

私の推測では、すべて問題ありませんが、問題は、gammaSlider の RightThumbValue プロパティを変更してもNumberCombineToパラメーターが に設定されることです。が変更されたときにプロパティを更新する必要があります。default (0) NumberCombineToRightThumbValue

4

1 に答える 1