使用数据绑定属性刷新对象
我想我在这里遗漏了一些非常基本的东西。
我创建了一个带有 DependencyProperty
的 UserControl
,定义如下:(
public static DependencyProperty SpinnerColourProperty = DependencyProperty.Register("SpinnerColour", typeof(Color), typeof(LoadingControl), new PropertyMetadata(Colors.Black));
public Color SpinnerColour
{
get
{
return (Color)GetValue(SpinnerColourProperty);
}
set
{
SetColour(value);
SetValue(SpinnerColourProperty, value);
}
}
是的,我更喜欢英式英语......所以告我吧:P)
SetColour
方法,正如预期的那样,改变了控件的外观。
如果在我的代码中编写 customControl1.SpinnerColour = Colors.Red
那么控件将正确更改属性和通过设置器应用的视觉效果的颜色。 (如果我在 Visual Studio 的设计器中使用该属性,我什至可以观察到此更改!)
但是,如果我将数据绑定应用于该属性,然后更改它绑定到的元素,则该属性的值将会更改,但setter 永远不会被调用,实际显示也永远不会改变。
例如,如果我定义一个按钮和我的控件:
<my:MyControl x:Name="myControl1" SpinnerColour="{Binding ElementName=button1, Path=Background.Color, Mode=TwoWay}" />
<Button Content="Button" Height="23" Name="button1" Click="button1_Click" />
并且 Click
事件会更改按钮背景的颜色,如下所示:
private void button1_Click(object sender, RoutedEventArgs e)
{
button1.Background = new SolidColorBrush(Colors.Red);
}
然后,如果我使用以下命令检查 SpinnerColour
属性调试器,它会是红色的。然而,正如我所写,设置器永远不会被调用,并且实际控件永远不会改变它的颜色。
我做错了什么?
I think I'm missing something very fundamental here.
I've created a UserControl
with a DependencyProperty
, defined as such:
public static DependencyProperty SpinnerColourProperty = DependencyProperty.Register("SpinnerColour", typeof(Color), typeof(LoadingControl), new PropertyMetadata(Colors.Black));
public Color SpinnerColour
{
get
{
return (Color)GetValue(SpinnerColourProperty);
}
set
{
SetColour(value);
SetValue(SpinnerColourProperty, value);
}
}
(Yes, I prefer British-English... so sue me :P)
The SetColour
method, as expected, changes how the control looks.
If in my code I write customControl1.SpinnerColour = Colors.Red
then the control will correctly change the colour both of the property and of the visual effect which is applied through the setter. (I can even observe this change if I play around with the property in Visual Studio's designer!)
However, if I apply a data binding to that property and then change the element it's bound to, the value of the property will change, but the setter will never be called and the actual display will never change.
So for example, if I define a button and my control:
<my:MyControl x:Name="myControl1" SpinnerColour="{Binding ElementName=button1, Path=Background.Color, Mode=TwoWay}" />
<Button Content="Button" Height="23" Name="button1" Click="button1_Click" />
And the Click
event changes the colour of the buttons background as such:
private void button1_Click(object sender, RoutedEventArgs e)
{
button1.Background = new SolidColorBrush(Colors.Red);
}
Then if I check the SpinnerColour
property using the debugger, it'll be red. However, as I wrote, the setter is never called, and the actual control never changes it's colour.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您滥用了依赖属性。
仅当您在代码中设置属性时,才会使用调用
SetValue
的本机 CLR 属性。绑定基础结构直接调用
SetValue
(而不是使用反射),因此您的代码永远不会运行。您不应该在 DependencyProeprty 包装器中放置除
GetValue
和SetValue
之外的任何代码。相反,您需要在 DependencyProperty 注册中指定
ValueChangedCallback
,如下所示PropertyMetadata
构造函数的第二个参数。You're misusing dependency properties.
The native CLR property that calls
SetValue
is only used when you set the property in code.The binding infrastructure calls
SetValue
directly (instead of using reflection), so your code never runs.You should never put any code other than
GetValue
andSetValue
in a DependencyProeprty wrapperInstead, you need to specify a
ValueChangedCallback
in the DependencyProperty registration, as the second parameter to thePropertyMetadata
constructor.