【问题标题】:DependencyProperty of type "Binding" is not being updated“Binding”类型的 DependencyProperty 未更新
【发布时间】:2011-12-01 17:52:11
【问题描述】:

我在创建“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}">

在我的“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); }
    }

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

我做错了什么?我已经看过很多这样的例子,我想这就是 DataGrid 自己必须做的事情。

谢谢

【问题讨论】:

    标签: c# .net wpf binding dependency-properties


    【解决方案1】:

    虽然@WPF-it 解决方案有效,但它不适合附加属性,因为您无法附加 CLR 属性。要解决这个问题,您可以像往常一样定义附加属性,并通过调用BindingOperations.GetBindingBase() 获取绑定对象。

    private static void OnMyPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Can also use GetBinding(), GetBindingExpression()
        // GetBindingExpressionBase() as needed.
        var binding = BindingOperations.GetBindingBase(d, e.Property);
    }
    

    【讨论】:

      【解决方案2】:

      奇怪的是,我所知道的唯一其他具有Binding 类型属性的地方是DataGridBoundColumn 类,它派生为DataGridTextColumnDataGridCheckBoxColumn 等...

      有趣的是,该属性不是依赖属性。它是一个普通的 CLR 类型属性。我猜绑定的基础设施是基于你不能绑定到绑定类型 DP 的限制。

      同一个类的其他属性都是很好的DP,比如Visibility,Header等。

      DataGridBoundColumn 中,Binding 属性声明如下,并对其进行了非常粗略的解释...

      这不是一个 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);
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-23
        • 1970-01-01
        • 2015-03-03
        • 1970-01-01
        • 2011-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多