繁体   English   中英

在DataBinding中访问属性的属性

[英]Accessing Properties of Properties in DataBinding

我的ViewModel中有两个公共属性FooBar Foo只是一个字符串, Bar是一个具有公共属性Name的类,它是一个字符串。

我想将Bar.Name绑定到某个GUI元素。 我怎么做?

<Label Content="{Binding Foo}">按预期将字符串Foo写入Label。

但是<Label Content="{Binding Bar.Name}">不会将Bar的名称写入Label。 相反,标签保持空白。

编辑:我的XAML的DataContext (以及Label的DataContext )设置为ViewModel。

编辑2:当然,真正的代码并不像上面描述的那么简单。 我构建了一个仅代表以上描述的最小工作示例:

XAML:

<Window x:Class="MyTestNamespace.MyXAML"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <StackPanel>
        <Label Content="{Binding Foo}"></Label>
        <Label Content="{Binding Bar.Name}"></Label> <!-- Works fine! -->
    </StackPanel>
</Window>

视图模型:

namespace MyTestNamespace
{
    class MyVM
    {
        public string Foo { get; set; }
        public MyBar Bar { get; set; }

        public MyVM()
        {
            Foo = "I am Foo.";
            Bar = new MyBar("I am Bar's name.");
        }
    }

    class MyBar
    {
        public string Name { get; set; }

        public MyBar(string text)
        {
            Name = text;
        }
    }
}

事实上这是按预期工作的。 由于我无法与您分享实际代码(太多并且由公司拥有),我需要自己搜索原因。 欢迎任何有关可能原因的提示!

1.Your Model.cs:

public class Model : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
             PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

2.您的ViewModel:

  public MainViewModel()
  {
     _model = new Model {Name = "Prop Name" };  
  }



  private Model _model;
  public Model Model
  {
      get
      {
          return _model;
      }
      set
      {
          _model = value;     
      }
  }

3.Your View,DataContext设置为ViewModel:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MainWindow" 
    DataContext="{StaticResource MainViewModel}">
<Grid>
    <Label Content="{Binding Model.Name}"/>
</Grid>

感谢Vignesh N.的评论,我能够解决问题。

在实际代码中Bar可以更改,但在开头它的名称是一个空字符串。 这是Label打开窗口时显示的内容。 由于Bar属性更改时不会通知Label ,因此不会更新。

解决方案:使ViewModel实现INotifyPropertyChanged接口并像这样定义Bar

private MyBar _bar;
public MyBar Bar
{
    get
    {
        return _bar;
    }

    set
    {
        if (_bar != value)
        {
            _bar = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bar)));
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM