4

2 つのテキスト ボックスがあり、1 つは最小値用、もう 1 つは最大値用です。他の機能が台無しになるため、お互いに propertychange イベントを呼び出すことはできません。ビュー モデルに IDataErrorInfo インターフェイスがあり、1 つのテキスト ボックスが検証されると、もう 1 つのテキスト ボックスも検証する必要があります。

たとえば、私が探している検証は、最小値が最大値よりも大きい場合にエラーが発生する必要がある場合ですが、このエラーはいずれかのテキストボックスから修正でき、これが発生した場合はテキストボックスのエラーを削除する必要があります。これは、最小プロパティと最大プロパティの両方で互いのプロパティ変更イベントを呼び出すことで簡単に実現できますが、他の機能が損なわれるため、これを行うことはできません。別の方法が必要です。何か案は?

     #region Implementation of IDataErrorInfo

            /// <summary>
            /// Gets the error message for the property with the given name.
            /// </summary>
            /// <returns>
            /// The error message for the property. The default is an empty string ("").
            /// </returns>
            /// <param name="columnName">The name of the property whose error message to get. </param>
            public string this[string columnName]
            {
                get
                {
                    string error = null;
                    if (columnName == Reflection.GetPropertyName<ColorPaletteManagerViewModel>(m => m.MaximumColorValue) ||
                        columnName == Reflection.GetPropertyName<ColorPaletteManagerViewModel>(m => m.MinimumColorValue))
                    {
                        error = GetTimeGateError();
                    }
                    _errors[columnName] = error;                              
                    OnPropertyChanged<ColorPaletteManagerViewModel>(m => m.Error);
                    return error;
                }
            }

            /// <summary>
            /// Gets an error message indicating what is wrong with this object.
            /// </summary>
            /// <returns>
            /// An error message indicating what is wrong with this object. The default is an empty string ("").
            /// </returns>
            public string Error
            {
                get
                {
                    string[] allErrors =
                        _errors.Where(i => !string.IsNullOrWhiteSpace(i.Value)).Select(m => m.Value).ToArray();
                    return string.Join(Environment.NewLine, allErrors);
                }
            }


            #endregion

    private string GetTimeGateError()
            {
                string error = null;

                if (MinimumColorValue > MaximumColorValue)
                {
                    error = string.Format("The Maximum Color Range Cannot be Less Then The Minimum Color Range");
                }

                return error;
            }

 /// <summary>
        /// Gets or sets the maximum color value.
        /// </summary>
        public float MaximumColorValue
        {
            get { return _maximumColorValue; } 
            set
            {
                if (_maximumColorValue != value)
                {
                    _maximumColorValue = value;
                    OnPropertyChanged(i => i.MaximumColorValue);
                }
            }
        }

        /// <summary>
        /// Gets or sets the minimum color value.
        /// </summary>
        public float MinimumColorValue
        {
            get { return _minimumColorValue; }
            set
            {
                if (_minimumColorValue != value)
                {
                    _minimumColorValue = value;
                    OnPropertyChanged(i => i.MinimumColorValue);
                }
            }
        }

xaml:

 <UserControl.Resources>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                            Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
            </Style>
    </UserControl.Resources>

     <TextBox Name="_minColorValue" 
                         Width="55" 
                         Height="22"
                         Grid.Row="1" 
                         Grid.Column="1" 
                         Margin="0,-4,0,0"
                         IsEnabled="{Binding ElementName=_override, Path=IsChecked}">
                    <TextBox.Text>
                        <Binding Path="MinimumColorValue" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged" Delay="500">
                            <Binding.ValidationRules>
                                <ValidationRules:NumberValidationRule/>
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

     <TextBox Name="_maxColorValue"
                         Width="55" 
                         Height="22"
                         Margin="0,-4,0,0"
                         HorizontalAlignment="Left" 
                         IsEnabled="{Binding ElementName=_override, Path=IsChecked}"
                         Grid.Column="1" 
                         Grid.Row="2">
                    <TextBox.Text>
                        <Binding Path="MaximumColorValue" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged" Delay="500">
                            <Binding.ValidationRules>
                                <ValidationRules:NumberValidationRule/>
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
4

2 に答える 2

1

これを解決するには、添付プロパティを使用することをお勧めします。

タイプ TextBox の添付プロパティを作成します。

その添付プロパティを最初の TextBox に配置し、x:Reference または Binding ElementName のいずれかを使用して、その値を 2 番目の TextBox の値に設定します。

同じ添付プロパティを別の TextBox に配置し、今回はその値を最初の TextBox の参照に設定します。

これで、各 TextBox は別の TextBox を認識します。それはあなたが勝ったことです。

ここで行う必要があるのは、ソースが更新されたとき、またはテキスト入力などで変更されたときに、別の TextBox を無効にするか変更することだけです。

ある人は常に他の人について知っているので、あなたは今彼らとやりたいことを何でもすることができます.

この提案はどうですか?これをコメントに投稿できればよかったのですが、収まりません。

于 2013-11-13T23:05:21.557 に答える