wpf - 依赖属性 : Getting but not Setting

标签 wpf dependency-properties

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 中绑定(bind)了一组依赖属性

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

最佳答案

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

其次,您不应在 SingleGridLength 属性的 setter 中引发 OnNotifyPropertyChanged。您只需对常规 CLR 属性执行此操作。依赖属性有一个内置的更改通知系统,绑定(bind) Hook 到其中。

关于wpf - 依赖属性 : Getting but not Setting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6031320/

相关文章:

wpf - 如何通过绑定(bind)在 ViewModel 中设置值?

c# - 如何将 Nullable 依赖属性设置为 Null?

c# - WPF 自定义控件 : Bind CollectionViewSource to DependencyProperty

java - Spring 依赖注入(inject) : FileNotFound Exception

c# - ListView 中分组标题的重复

wpf - 创建 MVVM 的模型部分

wpf - 如果我们不能绑定(bind) MouseBinding 的 Command,我们应该怎么做?

c# - WPF 动画结束时是否会触发任何事件?

c# - 是否可以将命令绑定(bind)到资源?

WPF 组框控件模板 : How to apply a Style only to elements in the Header?