windows phone 7 silverlight用户控件:数据绑定不起作用的自定义属性 [英] windows phone 7 silverlight user control: data binding not working on custom property
问题描述
我有一个相当简单的用户控件(RatingControl),它具有如下定义的依赖属性:
I have a rather simple user control (RatingControl) that has a dependency property defined as follows:
public partial class RatingControl : UserControl
{
public RatingControl()
{
InitializeComponent();
}
public static readonly DependencyProperty RatingValueProperty = DependencyProperty.Register("RatingValue", typeof(double), typeof(RatingControl), new PropertyMetadata(0.0));
public double RatingValue
{
set
{
double normalizeValue = 0.0;
if (value > 10.0)
{
normalizeValue = 10.0;
}
else if (value > 0.0)
{
normalizeValue = value;
}
SetValue(RatingValueProperty, normalizeValue);
RenderRatingValue();
}
get { return (double)GetValue(RatingValueProperty); }
}
...
如果我直接分配了此控件,则此控件会正确收到RatingValue:
This control receives properly the RatingValue if I assign it directly:
<gtcontrols:RatingControl RatingValue="2.0" />
但是,如果我尝试分配数据绑定,它不起作用。 RatingValue的set代码从未被调用,也不会在调试输出窗口中看到数据绑定错误。
请注意,我试图为标准属性(宽度)分配相同的值,在这种情况下,该值正确传递给它。
However, if I try to assign it with data binding, it does not work. The "set" code for RatingValue is never called, nor I see data binding errors in the debug output window. Note below that I tried to assign the same value to a standard property (Width), and in that case the value is properly passed to it.
<StackPanel>
<TextBox Name="Test" Text="200.0" />
<gtcontrols:RatingControl Width="{Binding ElementName=Test, Path=Text}" RatingValue="{Binding ElementName=Test, Path=Text}" />
<TextBlock Text="{Binding ElementName=Test, Path=Text}" />
</StackPanel>
不仅TextBlock正确接收值。还有RatingControl接收的是宽度,正确设置为200像素;然而,RatingValue未设置(方法设置甚至不调用)
Not only the TextBlock receives the value correctly. also RatingControl receives is for the width, properly set at 200 pixels; however, the RatingValue is not set (method set not even called)
我缺少什么?
提前感谢
What am I missing? Thanks in advance.
推荐答案
事情是绑定系统不使用CLR属性包装器(getter和setter)来分配依赖属性的值。那些只是为了方便,所以你可以使用属性,就像代码中的普通属性一样。在内部它使用SetValue()/ GetValue()方法。
The thing is that the binding system does not use the CLR property wrapper (getter and setter) to assign the value of a dependency property. Those are there just for convenience, so you could use the property just like a normal property in your code. Internally it uses SetValue()/GetValue() methods.
所以,值归一化的适当位置是依赖属性的属性更改的回调:
So, the proper place for the value normalization would be the property changed callback for the dependency property:
public static readonly DependencyProperty RatingValueProperty =
DependencyProperty.Register("RatingValue", typeof(double), typeof(RatingControl),
new PropertyMetadata(0.0, new PropertyChangedCallback(RatingValuePropertyChanged))));
static void RatingValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var ratingControl = (RatingControl)sender;
var val = (double)e.NewValue;
double normalizeValue = 0.0;
if (val > 10.0)
{
normalizeValue = 10.0;
}
else if (val > 0.0)
{
normalizeValue = val;
}
ratingControl.RatingValue = normalizeValue;
}
这篇关于windows phone 7 silverlight用户控件:数据绑定不起作用的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!