如何绑定List<string>到依赖属性 [英] How to bind List&lt;string&gt; to a DependencyProperty

查看:40
本文介绍了如何绑定List<string>到依赖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 DependencyProperty.为此,我想创建一个显示列表的新 UserControl.

I'm trying to learn about DependencyProperty. To do so I want to create a new UserControl which displays a list.

此列表的位置必须作为属性存在于父级中.为此,我只有 MainWindow、MainWindowViewModel(这些是父级)和 UserControl(子级)(目前正在使用代码隐藏).

The location of this list must exist in the parent as a property. For this, I only have MainWindow, MainWindowViewModel (these are the parent) and the UserControl (the child) (which is currently using code behind).

在我的主窗口中

<Grid>
    <uc:RecentList MessageList="{Binding Messages}" />    
</Grid>

在后面的代码中

public MainWindow()
{
    InitializeComponent();
    this.DataContext = new MainWindowViewModel();
}

和视图模型

public MainWindowViewModel()
{
    this.Messages = new ObservableCollection<string>();
    this.Messages.Add("Item 1");
    this.Messages.Add("Item 2");
    this.T = "hi";
}

public ObservableCollection<string> Messages { get; set; }

UserControl 我有

<Grid>
    <ListView ItemsSource="{Binding MessageList}"></ListView>
    <TextBlock Text="I'm such text to verify this control is showing" />
</Grid>

后面的代码是

public static readonly DependencyProperty MessageListProperty =
DependencyProperty.Register(
"MessageList", typeof(IEnumerable<string>), typeof(RecentList));

public IEnumerable<string> MessageList
{
    get { return (IEnumerable<string>)GetValue(MessageListProperty); }
    set { SetValue(MessageListProperty, value); }
}

我遇到的问题是绑定不起作用.我可以在输出窗口中看到这个,错误:

The issue I have is the binding is not working. I can see this in the Output Window, with the Error:

错误 40:BindingExpression 路径错误:在对象"MainWindowViewModel"(HashCode=26034861)上找不到MessageList"属性.BindingExpression:Path=MessageList;DataItem='MainWindowViewModel' (HashCode=26034861);目标元素是 'ListView' (Name='');目标属性是ItemsSource"(类型IEnumerable")

Error 40 : BindingExpression path error: 'MessageList' property not found on 'object' ''MainWindowViewModel' (HashCode=26034861)'. BindingExpression:Path=MessageList; DataItem='MainWindowViewModel' (HashCode=26034861); target element is 'ListView' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

我理解这个问题,但我对此感到困惑.它正在寻找正确的位置(在 MainWindowViewModel 中)但它看起来我不明白为什么 UserControl 正在寻找 MainWindowViewModel 中的 MessageList代码>.我想这是因为那是我设置数据上下文的地方,但是,我还认为如果我将 this.DataContext = this; 添加到 UserControl 的构造函数中,那么它是错误的(我已经尝试过了,它没有也行不通).

I understand the issue but I am confused by it. It's looking in the right place (in the MainWindowViewModel) but it is looking I don't understand why the UserControl is looking for the MessageList in the MainWindowViewModel. I guess it's because that is where I set the datacontext but, I also thought it that if I added this.DataContext = this; to the UserControl's constructor then it's wrong (I've tried it, it didn't work either).

将我的 UserControl 更新为

<ListView ItemsSource="{Binding MessageList, RelativeSource={RelativeSource Mode=TemplatedParent}}"></ListView>

帮助我没有收到错误消息,但我也没有看到结果.

Helps in the sense I don't get the error message, but I also don't see the result.

这就是我认为应用程序加载时发生的情况:

This is what I think is happening when the application loads:

  1. 主窗口加载
  2. MainWindow 然后看到 UserControl 并注意到它需要一个属性.
  3. 在 WPF 调用 UserControl 构造函数之前,它会获取属性的值.然后它初始化组件并自动将值推送到 UserControl 的属性

我的 UserControl 如何使用Parents (MainWindow) 属性(Messages)

How can my UserControl use the Parents (MainWindow) property (Messages)

推荐答案

UserControl 的 XAML 中的 Binding 应该将 UserControl 实例作为其源对象,例如像这样:

The Binding in the UserControl's XAML should have the UserControl instance as its source object, e.g. like this:

<ListView ItemsSource="{Binding MessageList,
    RelativeSource={RelativeSource AncestorType=UserControl}}" />

或者,您可以在 UserControl 上设置 x:Name 并使用 ElementName 绑定:

Alternatively you could set x:Name on the UserControl and use an ElementName binding:

<UserControl ... x:Name="self">
    ...
    <ListView ItemsSource="{Binding MessageList, ElementName=self}" />
    ...
</UserControl>

<小时>

此外,您通常不应将 UserControl 的 DataContext 设置为自身(如 DataContext = this;),因为这将有效防止从 UserControl 的父元素继承 DataContext,这对于外部"绑定工作,如:


Besides that you should usually not set the DataContext of the UserControl to itself (like DataContext = this;) because that would effectively prevent inheriting the DataContext from the UserControl's parent element, which is necessary for an "external" binding to work, like:

<uc:RecentList MessageList="{Binding Messages}" />    

这篇关于如何绑定List<string>到依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆