"Binding"类型的DependencyProperty未更新

Mar*_*tin 4 .net c# wpf binding dependency-properties

我在创建"Binding"类型的DependencyProperty时遇到了麻烦.其他类型工作正常,如果我使用绑定填充它们,它们会成功解析.

在我的场景中,我想获取原始绑定,以便我可以使用它来绑定到子对象的属性,就像DataGrid列的相同 - 即对于列中指定的每个绑定,它绑定到每个ItemsSource集合中的项目,而不是绑定DataContext本身.

<mg:MultiSelectDataGrid x:Name="Grid" DockPanel.Dock="Left" 
     ItemsSource="{Binding Path=Rows}" DataContext="{Binding}" 
     AutoGenerateColumns="False" UriBinding="{Binding Path=UrlItems}">
Run Code Online (Sandbox Code Playgroud)

在我的"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); }
    }
Run Code Online (Sandbox Code Playgroud)

永远不会调用回调,并且永远不会设置属性.我已经尝试过各种各样的排列,没有回调.唯一让我取得成功的是如果我用字符串替换绑定(例如UriBinding ="hello") - 在这种情况下它会触发回调,并设置属性,但当然会失败,因为它是错误的类型.

我究竟做错了什么?我已经看到了一大堆这样的例子,我想这就是DataGrid必须做的事情.

谢谢

WPF*_*-it 8

奇怪的是只有其他地方我知道有Bindingtype属性是DataGridBoundColumn类派生成DataGridTextColumn,DataGridCheckBoxColumn等...

有趣的是,该属性不是依赖属性.它是一个普通的CLR类型属性.我想绑定的基础结构是基于你无法绑定到绑定类型DP的限制.

同一类的其他属性非常好,如可见性,标题等.

DataGridBoundColumnBinding财产声明如下,同一个非常粗略的解释...

这不是DP,因为如果它获得该值将评估绑定.

    /// <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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)