无法将ItemsSource绑定到ElementName [英] Cannot bind ItemsSource to ElementName

查看:92
本文介绍了无法将ItemsSource绑定到ElementName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是我无法使用ItemsSource来做一个简单的例子.我的XAML:

This is probably a silly question, but I can't make a simple example with ItemsSource work. My XAML:

<Window x:Class="TestDataGrid.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"
        xmlns:local="clr-namespace:TestDataGrid"
        mc:Ignorable="d" Height="250" Width="300" Name="MyWindow">
    <ListBox ItemsSource="{Binding MyItems, ElementName=MyWindow}" Background="{Binding MyBrush, ElementName=MyWindow}"/>
</Window>

代码:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private ObservableCollection<Item> items;

    public MainWindow()
    {
        InitializeComponent();
        items = new ObservableCollection<Item>();
        items.Add(new Item { Key = "Key1", Value = "Value1" });
        items.Add(new Item { Key = "Key2", Value = "Value2" });
        items.Add(new Item { Key = "Key3", Value = "Value3" });
    }

    public ObservableCollection<Item> MyItems
    {
        get { return items; }
    }

    public Brush MyBrush
    {
        get { return Brushes.LightPink; }
    }
}

public class Item
{
    public string Key { get; set; }
    public string Value { get; set; }
}

如果我在代码中设置ItemsSource,或者设置了DataContext=this,然后从绑定中删除ElementName,则它起作用.但是,为什么它不能与ElementName一起使用? 我可以像这样绑定背景色,但不能绑定.

It works if I set ItemsSource in code, or if I set DataContext=this and then remove ElementName from my binding. But why doesn't it work with ElementName? I can bind the background color like this, but not collection.

我知道如何使其与DataContext或在代码隐藏中一起使用,但是我对为什么这个特定示例不起作用,我是否有所遗漏感兴趣?

I know how to make it work with DataContext, or in code-behind, but I'm interested in why this particular example doesn't work, am I missing something?

推荐答案

我知道如何使其与DataContext或在代码隐藏中一起使用,但是我对为什么这个特定示例不起作用,我是否有所遗漏感兴趣?

I know how to make it work with DataContext, or in code-behind, but I'm interested in why this particular example doesn't work, am I missing something?

@Clemens指出,如果在调用InitializeComponent()之前的中填充源集合,则绑定实际上将起作用.

As pointed out by @Clemens your binding will actually work if you populate the source collection before the InitializeComponent() is called.

在后面的代码中设置DataContext与在XAML中创建ElementName绑定之间的区别在于,后者已在InitializeComponent调用期间建立.以后添加到未实现INotifyCollectionChanged的集合中的项目将被忽略.

The difference between setting a DataContext in code behind and creating an ElementName Binding in XAML is that the latter is already established during the InitializeComponent call. Items that are added later - to a collection that does not implement INotifyCollectionChanged - are ignored.

public MainWindow()
{
    items = new ObservableCollection<Item>();
    items.Add(new Item { Key = "Key1", Value = "Value1" });
    items.Add(new Item { Key = "Key2", Value = "Value2" });
    items.Add(new Item { Key = "Key3", Value = "Value3" });
    InitializeComponent();
}

尽管如此,您也可以使用RelativeSource绑定到父窗口,

You could also bind to the parent window using a RelativeSource though, This works:

<ListBox ItemsSource="{Binding MyItems, RelativeSource={RelativeSource AncestorType=Window}}"/>

这篇关于无法将ItemsSource绑定到ElementName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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