【问题标题】:Set binding with unknown objects设置与未知对象的绑定
【发布时间】:2019-10-22 22:16:14
【问题描述】:

我正在尝试设置绑定传递属性作为参数,但我找不到如何做到这一点的方法。 这有效:

Binding bind = new Binding();
//Some code
var tb = Control as TextBlock;
tb.SetBinding(TextBlock.TextProperty, bind );

我想做的工作与此类似:

public FrameworkElement Control {get;set;}
public string dp {get;set;}
public string TypeOfControl {get;set;}    

var tb = Control as typeof(TypeOfControl);
tb.SetBinding(typeof(TypeOfControl).dp, bind );

我已经尝试过这样做: DependencyProperty from string

        var descriptor = DependencyPropertyDescriptor.FromName(dp,typeof(TextBlock), typeof(Control));
        descriptor.SetValue(Control, bind);

但我从描述符中得到空值。

【问题讨论】:

    标签: c# binding dependency-properties system.reflection


    【解决方案1】:

    这似乎有效:

            Type type = Control.GetType();
            var descriptor = DependencyPropertyDescriptor.FromName(DependencyPropertyName, type, type);
            var dp = descriptor.DependencyProperty;
            Control.SetBinding(dp, Bind);
    

    在我的情况下(使用 TextBlock 测试)的 DependencyPropertyName 是“文本”。我认为它必须是“TextProperty”。 这就是描述符始终为空的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多