更新绑定依赖属性



我有一个自定义用户控件,我必须扩展以添加一些新元素。在此控件中,我已经有几个属性:

public static readonly DependencyProperty CountProperty =
        DependencyProperty.Register("CountProperty ", typeof(int), typeof(SomeThirdPartyControl), new PropertyMetadata(0));
    public int Count
    {
        get { return (int)GetValue(CountProperty ); }
        set
        {
            SetValue(CountProperty, value);
        }
    }

并添加了类似的项目

var textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
            textBlockFactory.SetValue(TextBlock.TextProperty, new Binding(nameof(Count)));

我也有一种更新方法,基本上可以做类似的事情:

Count = items.Count;

当计数更新后,我希望UI已更新。但是,textBlockFactory中的值似乎从未得到更新。

更改依赖项属性时,如何确保更新该Frameworkelement值。

尝试将依赖性依赖性设置为默认情况下绑定Twoway,如@arie所说:

类似的东西:

        public int Count
        {
            get { return (int)GetValue(CountProperty); }
            set { SetValue(CountProperty , value); }
        }
        public static readonly DependencyProperty CountProperty =
            DependencyProperty.Register("Count" , typeof(int) , 
                typeof(SomeThirdPartyControl) ,
                  new FrameworkPropertyMetadata(0 ,
                       FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

最新更新