绑定到ItemsControl仅适用于后面的代码

时间:2015-01-16 15:10:00

标签: c# wpf xaml binding observablecollection

我正在尝试将ObservableCollection绑定到ItemsControl。我创建了一个名为Persons的类,它有一些属性。然后我创建了一个ObservableCollection并在那里添加了一些人。

如果我给ItemsControl一个x:Name =“PersonH​​older”并使用PersonH​​older.ItemsSource = people添加ItemsSource,那么一切正常。但是,如果我试图在XAML中添加具有绑定的人......没有结果。

这是XAML:

<StackPanel Background="White">
    <ItemsControl ItemsSource="{Binding persons}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

C#中的一些代码

public ObservableCollection<Person> persons { get; set; }
public MainWindow()
{
    InitializeComponent();
    persons = new ObservableCollection<Person>();
    persons.Add(new Person { Name = "Peter" });
}

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

希望你能帮助我。

最好的问候。

3 个答案:

答案 0 :(得分:2)

默认情况下,绑定将搜索当前DataContext中的属性。您需要设置绑定上下文,例如通过在代码中手动设置DataContext。但是,由于persons未引发PropertyChanged事件,因此您需要在创建DataContext后设置persons,否则将不会通知用户属性已更改

InitializeComponent();
persons = new ObservableCollection<Person>();
//after persons is created
DataContext = this;

未实施INotifyPropertyChanged并将其保留在XAML中,您可以保留人的单个实例

private readonly ObservableCollection<Person> _persons = new ObservableCollection<Person>();

public ObservableCollection<Person> persons { get { return _persons; } }

public MainWindow()
{
    InitializeComponent();      
    persons.Add(new Person { Name = "Peter" });
}

和XAML

<Window ... x:Name="window">
    <!-- .... -->
    <ItemsControl ... ItemsSource="{Binding ElementName=window, Path=persons}">

答案 1 :(得分:1)

你必须将它绑定到控件本身:

 <ItemsControl ItemsSource="{Binding persons,  
               RelativeSource={RelativeSource AncestorType={x:Type Window}}}">

否则它会立即绑定已设置为null的DataContext属性。您还可以在代码中设置DataContext,例如DataContext = this

所有绑定错误都在“输出”窗口中。

答案 2 :(得分:0)

我宁愿建议以下实施。

窗口:

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        var context = new SomeContext();
        DataContext = context;
        context.AddPerson();
    }
}

上下文:

public class SomeContext 
{
    public ObservableCollection<Person> People { get; set; }

    public SomeContext()
    {
        People = new ObservableCollection<Person>();
        People.Add(new Person { Name = "Samuel" });
    }

    public void AddPerson()
    {
        People.Add(new Person { Name = "Peter" });
    }
}

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

XAML:

<Window x:Class="BindingTest.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" Height="350" Width="525">
    <Grid>
        <StackPanel Background="White">
            <ItemsControl ItemsSource="{Binding People}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
    </Grid>
</Window>