【发布时间】:2015-04-23 02:50:17
【问题描述】:
我正在使用 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 验证规则:
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); }
}
}
我的猜测是,一切都很好,但问题是,NumberCombineTo 参数设置为default (0) 即使我更改了 gammaSlider 的 RightThumbValue 属性。
当RightThumbValue 更改时,我需要更新NumberCombineTo 属性。
【问题讨论】:
-
很难说没有a good, minimal, complete code example。你看过调试器的“输出”窗口吗?那里有使用诊断信息吗?您是否确认绑定到
RightThumbValue在ValidationRule场景之外有效?您发布的代码对我来说似乎没问题,但这并没有说明什么;我在 WPF 中尝试了很多看起来应该可以工作的东西,但实际上却没有。 :) -
我已经逐行调试了代码。
RightThumbValue正在发生应有的变化。我认为验证的 binding 部分是错误的。因为NumberCombineTo没有改变。即使RightThumbValue已更改,它也具有DependencyProperty's默认值。 -
再次:您是否在调试器的“输出”窗口中看到任何诊断消息?绑定失败通常会生成某种有用的消息。查看您的代码,唯一值得怀疑的是您对
LeftThumbValue和RightThumbValue绑定使用了不同的ElementName值。当然,如果您实际上有两个不同的双拇指滑块,这可能没问题;但如果您希望两者都绑定到同一个滑块,则使用两个不同的名称可能是错误的(即使用gammaSlider或myOwnSlider,但不能同时使用两者)。 -
我很抱歉其中两个是“gammaSlider”。 gammaSlider 类似于THIS ONE
标签: c# wpf validationrules