【问题标题】:Dependency Property: Getting but not Setting依赖属性:获取但不设置
【发布时间】:2012-03-19 18:06:20
【问题描述】:
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 中绑定了一组依赖属性

我遇到的问题是每个属性都是“获取”并返回值,但它从不“设置”值。我可以在转换器中使用依赖属性吗?或者我应该以不同的方式来解决这个问题?提前致谢!

【问题讨论】:

    标签: wpf dependency-properties


    【解决方案1】:

    首先,您的绑定无效。您正在将 SingleGridLength 属性绑定到自身。您需要将其绑定到另一个属性/对象。

    其次,您不应在 SingleGridLength 属性的设置器中提高 OnNotifyPropertyChanged。您只需对常规 CLR 属性执行此操作。依赖属性有一个内置的更改通知系统,绑定挂钩。

    【讨论】:

      【解决方案2】:

      查看您可以在PropertyMetadata 构造函数中指定的PropertyChangedCallback 委托。当你的依赖属性的属性值发生变化时,回调将被调用,你可以将你的处理代码放在这个回调方法中。

      【讨论】:

        【解决方案3】:

        我建议使用基于 IValueConverter 的转换器? 然后转换器应该只进行从输入到输出格式的计算。转换器返回的值

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        

        将由您进行绑定的属性使用。

        见:http://msdn.microsoft.com/de-de/library/system.windows.data.ivalueconverter.aspx

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-19
          • 2014-08-14
          • 2016-01-08
          • 1970-01-01
          相关资源
          最近更新 更多