【问题标题】:WPF textblock binding in XAMLXAML 中的 WPF 文本块绑定
【发布时间】:2013-10-08 12:44:47
【问题描述】:

我正在更新一些现有的 WPF 代码,我的应用程序有许多这样定义的文本块:

<TextBlock x:Name="textBlockPropertyA"><Run Text="{Binding PropertyA}"/></TextBlock>

在这种情况下,“PropertyA”是我的业务类对象的属性,定义如下:

public class MyBusinessObject : INotifyPropertyChanged
{
    private void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }

    private string _propertyA;
    public string PropertyA
    {
        get { return _propertyA; }
        set
        {
            if (_propertyA == value)
            {
                return;
            }

            _propertyA = value;
            OnPropertyChanged(new PropertyChangedEventArgs("PropertyA"));
        }
    }

    // my business object also contains another object like this
    public SomeOtherObject ObjectA = new SomeOtherObject();

    public MyBusinessObject()
    {
        // constructor
    }
}

现在我有一个 TextBlock,我需要将它绑定到 ObjectA 的一个属性,如您所见,它是 MyBusinessObject 中的一个对象。在代码中,我将其称为:

MyBusinessObject.ObjectA.PropertyNameHere

与我的其他绑定不同,“PropertyNameHere”不是 MyBusinessObject 的直接属性,而是 ObjectA 上的属性。我不确定如何在 XAML 文本块绑定中引用它。谁能告诉我我会怎么做?谢谢!

【问题讨论】:

    标签: c# wpf xaml data-binding textblock


    【解决方案1】:

    在&lt;Run Text="{Binding ObjectA.PropertyNameHere}" /&gt; 起作用之前,您必须将ObjectA 本身设置为属性,因为绑定仅适用于属性而不是字段。

    // my business object also contains another object like this
    public SomeOtherObject ObjectA { get; set; }
    
    public MyBusinessObject()
    {
        // constructor
        ObjectA = new SomeOtherObject();
    }
    

    【讨论】:

      【解决方案2】:

      你可以简单地输入这个:

      <TextBlock Text="{Binding ObjectA.PropertyNameHere"/>
      

      您可能希望在您的ObjectA 类中实现INotifyPropertyChanged,因为您的MyBusinessObject 类中的PropertyChanged 方法不会拾取该类的更改属性。

      【讨论】:

        【解决方案3】:

        尝试以与 PropertyA 相同的方式实例化 ObjectA(即,作为属性,具有公共 getter/setter,并调用 OnPropertyChanged),那么您的 XAML 可以是:

        <TextBlock Text="{Binding ObjectA.PropertyNameHere}" />
        

        【讨论】:

          【解决方案4】:

          您可以像为PropertyA 做的那样做同样的事情,如下所示,

          OnPropertyChanged(new PropertyChangedEventArgs("ObjectA"));
          

          关于设计器 XAML,

          <TextBlock x:Name="ObjectAProperty" Text="{Binding ObjectA.PropertyNameHere}" />
          

          【讨论】:

          • 是的,我知道,在属性中,他没有调用属性更改事件...所以首先包含它并添加了 xaml
          【解决方案5】:

          试试这个:

          在代码中:

          public MyBusinessObject Instance { get; set; }
          
          Instance = new MyBusinessObject();
          

          在 XAML 中:

          <TextBlock Text="{Binding Instance.PropertyNameHere" />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-01-29
            • 2018-06-10
            • 1970-01-01
            • 2023-04-09
            • 2011-07-15
            • 1970-01-01
            • 1970-01-01
            • 2017-08-20
            相关资源
            最近更新 更多