依赖属性:获取但不设置
public static readonly DependencyProperty SingleGridLengthProperty = DependencyProperty.Register("SingleGridLength", typeof(double), typeof(MapConverter));
public class MapConverter : DependencyObject, INotifyPropertyChanged, IMultiValueConverter
{
public double SingleGridLength
{
get { return (double)GetValue(MapConverter.SingleGridLengthProperty); }
set
{
SetValue(MapConverter.SingleGridLengthProperty, value);
OnNotifyPropertyChanged("SingleGridLength");
}
}
<local:MapConverter x:Key="MapConverter"
SingleGridLength="{Binding SingleGridLength, RelativeSource={RelativeSource Self}}" />
我有一个转换器,在 .xaml 中绑定了一组依赖属性。
我遇到的问题是每个属性都“获取”并返回值,但它从不“设置”该值。我可以在转换器中使用依赖属性吗?或者我应该以不同的方式处理这个问题?提前致谢!
public static readonly DependencyProperty SingleGridLengthProperty = DependencyProperty.Register("SingleGridLength", typeof(double), typeof(MapConverter));
public class MapConverter : DependencyObject, INotifyPropertyChanged, IMultiValueConverter
{
public double SingleGridLength
{
get { return (double)GetValue(MapConverter.SingleGridLengthProperty); }
set
{
SetValue(MapConverter.SingleGridLengthProperty, value);
OnNotifyPropertyChanged("SingleGridLength");
}
}
<local:MapConverter x:Key="MapConverter"
SingleGridLength="{Binding SingleGridLength, RelativeSource={RelativeSource Self}}" />
I have a converter with a set of dependency properties bound in the .xaml
The problem I am having is each property is "getting" and returning the value but it never "sets" the value. Am I allowed to use dependency properties in converters? Or should I be approaching this a different way? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,你的绑定无效。您正在将 SingleGridLength 属性绑定到其自身。您需要将其绑定到另一个属性/对象。
其次,您不应该在 SingleGridLength 属性的 setter 中引发 OnNotifyPropertyChanged。您只需对常规 CLR 属性执行此操作。依赖属性具有绑定挂钩的内置更改通知系统。
First, your binding is invalid. You are binding the SingleGridLength property to itself. You would need to bind it to another property/object.
Second, you shouldn't raise the OnNotifyPropertyChanged in the setter for your SingleGridLength property. You only need to do that for regular CLR properties. Dependency properties have a built in change notification system that Binding hooks into.
查看您可以指定的 PropertyChangedCallback 委托在 PropertyMetadata构造函数。当依赖属性的属性值发生更改时,将调用回调,并且您可以将处理代码放入此回调方法中。
Take a look at the PropertyChangedCallback delegate that you can specify in the PropertyMetadata constructor. The callback will be invoked when the property value of your dependency property changes, and you can place your handling code within this callback method.
我建议使用基于 IValueConverter 的转换器?
然后转换器应该只进行从输入到输出格式的计算。转换器返回的值
将由
您进行绑定的属性使用。
请参阅:http://msdn.microsoft.com/de -de/library/system.windows.data.ivalueconverter.aspx
I suggest using a Converter basing on IValueConverter?
The converter then should only do the calculation from the input to the output format. The value returned by the converters
and
will be used by the property you did the binding on.
See: http://msdn.microsoft.com/de-de/library/system.windows.data.ivalueconverter.aspx