的DependencyProperty类型&QUOT的;结合"未更新类型、DependencyProperty、QUOT

2023-09-03 15:24:43 作者:七厘米的蔚蓝

我有创造型的DependencyProperty绑定的麻烦。其他类型的工作确定,他们成功地解决,如果我填充它们使用的绑定。

在我的情况我要抢原料的绑定,这样我就可以用它来绑定到子对象的属性,在大致相同的方式,DataGrid中不列 - 即每一个绑定列中的指定,则结合中的每个的ItemsSource集合中的项,而不是结合在DataContext本身。

 < MG:MultiSelectDataGrid X:名称=网格DockPanel.Dock =左
     的ItemsSource ={绑定路径=行}的DataContext ={结合}
     的AutoGenerateColumns =FALSEUriBinding ={绑定路径= UrlItems}>
 

和我的MultiSelectDataGrid:

 公共静态只读的DependencyProperty UriBindingProperty =
       DependencyProperty.Register(UriBinding的typeof(BindingBase)
           typeof运算(MultiSelectDataGrid)
           新PropertyMetadata {PropertyChangedCallback = OnBindingChanged});


    私有静态无效OnBindingChanged(DependencyObject的研发,
                            DependencyPropertyChangedEventArgs五)
    {
         //这是从来没有enterred
    }


    公共BindingBase UriBinding
    {
        {返回(BindingBase)的GetValue(UriBindingProperty); }
        集合{的SetValue(UriBindingProperty,价值); }
    }
 
成果 张阳 公司信用类债券信息披露制度的统合建构 以偿债能力为中心展开

回调不会被调用,并且属性永远不会被设置。我已经试过各种排列,有回调,没有。这给了我任何成功的唯一的事情是,如果我替换字符串的结合(如UriBinding =你好) - 在这种情况下,将触发回调,并设置该属性,但当然会,失败,因为它是错误类型。

我是什么做错了吗?我见过这样的例子整个负载,我想这是DataGrid中必须做自己。

感谢

解决方案

奇怪的是唯一的其他地方我知道,有绑定键入属性是 DataGridBoundColumn 类派生到 DataGridTextColumn DataGridCheckBoxColumn 等等...

有趣的是那里的属性不是一个依赖项属性。这是一个普通的CLR类型属性。我想结合的infrastructre是基于你不能绑定到绑定类型DP。

限制

在同一类的其他性能都非常好喜欢能见度DPS,页眉等。

DataGridBoundColumn 绑定属性的声明如下一个非常粗糙的解释一样...

  

这不是一个DP因为如果它获取值将评估   结合。

  ///<总结>
    ///将被应用到生成的元素的结合。
    ///< /总结>
    ///<说明>
    ///这不是一个DP因为如果它得到的值将评估结合。
    ///< /说明>
    公共虚拟BindingBase绑定
    {
        得到
        {
            如果(!_bindingEnsured)
            {
                如果(!的IsReadOnly)
                {
                    DataGridHelper.EnsureTwoWayIfNotOneWay(_binding);
                }

                _bindingEnsured = TRUE;
            }

            返回_binding;
        }

        组
        {
            如果(_binding!=值)
            {
                BindingBase oldBinding = _binding;
                _binding =价值;
                CoerceValue(IsReadOnlyProperty);
                CoerceValue(SortMemberPathProperty);
                _bindingEnsured = FALSE;
                OnBindingChanged(oldBinding,_binding);
            }
        }
    }
 

I'm having trouble creating a DependencyProperty of type "Binding". Other types work ok, and they resolve successfully if I populate them using a binding.

In my scenario I want to grab the raw binding, so that I can use it to bind to properties of child objects, in much the same way that DataGrid does columns - ie for each binding specified in a column, it binds to each of the items in the ItemsSource collection, rather than binding the the DataContext itself.

<mg:MultiSelectDataGrid x:Name="Grid" DockPanel.Dock="Left" 
     ItemsSource="{Binding Path=Rows}" DataContext="{Binding}" 
     AutoGenerateColumns="False" UriBinding="{Binding Path=UrlItems}">

And in my "MultiSelectDataGrid":

    public static readonly DependencyProperty UriBindingProperty = 
       DependencyProperty.Register("UriBinding", typeof(BindingBase),
           typeof(MultiSelectDataGrid), 
           new PropertyMetadata { PropertyChangedCallback = OnBindingChanged});


    private static void OnBindingChanged(DependencyObject d,
                            DependencyPropertyChangedEventArgs e)
    {
         // This is never enterred
    }


    public BindingBase UriBinding
    {
        get { return (BindingBase)GetValue(UriBindingProperty); }
        set { SetValue(UriBindingProperty, value); }
    }

The callback never gets called, and the property never gets set. I've tried all kinds of permutations, with callbacks, without. The only thing that gave me any success was if I replaced the binding with a string (eg UriBinding="hello") - in that case it would fire the callback, and set the property, but would, of course, fail because it's the wrong type.

What am I doing wrong? I've seen a whole load of examples of this, and I guess this is what DataGrid must be doing itself.

Thanks

解决方案

Curiously only other place I am aware of that has Binding type property is DataGridBoundColumn class which derives into DataGridTextColumn, DataGridCheckBoxColumn etc...

And interestingly there the property is NOT a dependency property. It is a plain CLR type property. I guess the infrastructre of binding is based upon the limitation that you cannot bind to binding type DP.

Other properties of the same class are very well DPs like Visibility, Header etc.

In DataGridBoundColumn the Binding property is declared as below with a very crude explanation for the same ...

This isn't a DP because if it were getting the value would evaluate the binding.

    /// <summary>
    ///     The binding that will be applied to the generated element.
    /// </summary>
    /// <remarks>
    ///     This isn't a DP because if it were getting the value would evaluate the binding.
    /// </remarks>
    public virtual BindingBase Binding
    {
        get
        {
            if (!_bindingEnsured)
            {
                if (!IsReadOnly)
                {
                    DataGridHelper.EnsureTwoWayIfNotOneWay(_binding);
                }

                _bindingEnsured = true;
            }

            return _binding;
        }

        set
        {
            if (_binding != value)
            {
                BindingBase oldBinding = _binding;
                _binding = value;
                CoerceValue(IsReadOnlyProperty);
                CoerceValue(SortMemberPathProperty);
                _bindingEnsured = false;
                OnBindingChanged(oldBinding, _binding);
            }
        }
    }