我正在用C#和WPF开发一个应用程序,我有自己的滑块自定义控件。以及同一窗口上的文本框。我的滑块的所有属性都是DependencyProperty。
我使用文本框来更改滑块的属性。我想在文本框上使用ValidationRule。我编写了自己的ValidationRule (源自ValidationRule类)。我想把一些参数传递给那个ValidationRule。这是代码:
TextBox:
<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的NumberCombineTo属性,也会将它设置为default (0) 。我需要在更改NumberCombineTo属性时更新RightThumbValue属性。
发布于 2015-02-21 23:29:52
我根据您在这里提供的代码片段编写了一个简单的完整代码示例,我相信我能够重现您正在经历的基本问题。
如果在调试器中运行代码,并查看"Output“窗口,您可能会看到一条部分读取的消息:
找不到控制目标元素的FrameworkElement或FrameworkContentElement
WPF绑定系统需要其中一个元素才能查找源元素的名称(即ElementName属性的对象)。但是在这个场景中,您试图以WPF可见的方式绑定对象的属性,该对象本身既不是与框架相关的元素,也不是与框架相关的元素。因此,当试图配置绑定时,它会失败。
我看过几篇不同的文章,建议通过一个“代理对象”来解决这个问题。这通常遵循声明绑定到包含对象的DataContext的资源的模式,然后使用该对象作为绑定的Source。但是,在我看来,要正确设置这个设置似乎很棘手,这取决于是否能够设置一个特定的DataContext对象,该对象包含您实际上希望绑定到的属性。随着没有框架元素的绑定数量的增加,您可以在多大程度上使用这种技术是有限度的。
例如:
如何在DataContext未被继承时绑定到数据
在WPF中将虚拟分支附加到逻辑树
即使在这里,WPF错误:找不到目标元素的FrameworkElement
相反,在我看来,这是代码隐藏更好的情况之一。它只是几行代码来显式地设置绑定,它完全避免了WPF实际上是否能够找到要绑定到的对象的问题。
最后,我得到了一个XAML示例,如下所示:
<Window x:Class="TestSO28645688ValidationRuleParameter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestSO28645688ValidationRuleParameter"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ValidationParameters x:Key="validationParams1"
ValTypeFor0to255="ShouldBeSmaller"/>
</Window.Resources>
<StackPanel>
<Slider x:Name="slider1" Value=".25" />
<Slider x:Name="slider2" Value=".5"/>
<TextBlock Text="{Binding ElementName=slider1, Path=Value,
StringFormat=slider1.Value: {0}}" />
<TextBlock Text="{Binding ElementName=slider2, Path=Value,
StringFormat=slider2.Value: {0}}" />
<TextBlock Text="{Binding Source={StaticResource validationParams1},
Path=NumberCombineTo,
StringFormat=validationParams1.NumberCombineTo: {0}}" />
<TextBox x:Name="textBox1" HorizontalAlignment="Left" VerticalAlignment="Top"
Height="24" Width="400"
Margin="5" TextWrapping="Wrap"
MaxLength="3" HorizontalContentAlignment="Center" TabIndex="2">
<TextBox.Text>
<Binding UpdateSourceTrigger="PropertyChanged" Mode="TwoWay"
ElementName="slider1" Path="Value" NotifyOnValidationError="True"
ValidatesOnExceptions="True" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<ExceptionValidationRule/>
<local:ZeroTo255MinMax Parameters="{StaticResource validationParams1}"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>
</Window>这里与代码不同的主要地方是,我将ValidationParameters对象放在窗口的资源中。这使我可以轻松地从代码背后引用它,以便在那里绑定。
(当然,其余的代码也是不同的,但没有任何有意义的方式。我觉得用两个单独的Slider控件作为基本示例比较简单,因为这是内置在WPF中的,并且在窗口中提供了TextBlock元素,以便更容易地查看正在发生的事情)。
代码隐藏如下所示:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Binding binding = new Binding();
binding.Source = slider2;
binding.Path = new PropertyPath(Slider.ValueProperty);
ValidationParameters validationParams = (ValidationParameters)Resources["validationParams1"];
BindingOperations.SetBinding(validationParams, ValidationParameters.NumberCombineToProperty, binding);
}
}换句话说,它只是创建一个新的Binding对象,分配源对象和属性,检索要绑定该object+property的ValidationParameters对象,然后在ValidationParameters对象的NumberCombineTo属性上设置绑定。
当我这样做时,NumberCombineTo属性将随着slider2.Value值的变化而正确更新,并且它可以在调用ValidationRule对象的Validate()方法时使用。
https://stackoverflow.com/questions/28645688
复制相似问题