在XAML中而不是在代码隐藏中设置DataContext

本文关键字:隐藏 DataContext 设置 代码 XAML | 更新日期: 2023-09-27 18:25:34

我在其他文章中读到,可以在XAML中指定DataContext,而不是在其背后的代码中指定DataContext。

我在主类的构造函数中声明并填充了一个ObservableCollection,我还在其中设置了DataContext:

using System;
using System.Windows;
using System.Collections.ObjectModel;
namespace ItemsControlDemo
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<Person> PersonList { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            PersonList = new ObservableCollection<Person>();
            PersonList.Add(new Person(16, "Abraham", "Lincoln"));
            PersonList.Add(new Person(32, "Franklin", "Roosevelt"));
            PersonList.Add(new Person(35, "John", "Kennedy"));
            PersonList.Add(new Person(2, "John", "Adams"));
            PersonList.Add(new Person(1, "George", "Washington"));
            PersonList.Add(new Person(7, "Andrew", "Jackson"));
            DataContext = this;
        }
        private void Button_Add_Click(object sender, RoutedEventArgs e)
        {
            PersonList.Add(new Person(3, "Thomas", "Jefferson"));
        }
        private void Button_Remove_Click(object sender, RoutedEventArgs e)
        {
            PersonList.Remove(TheDataGrid.SelectedItem as Person);
        }
    }
    public class Person
    {
        public Person() { }
        public Person(int id, string firstName, string lastName)
        {
            ID = id;
            FirstName = firstName;
            LastName = lastName;
        }
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

如果我这样做,在我的应用程序中一切都很好。

但是,如果我从构造函数中删除"DataContext=this;",而是在应用程序的Window元素中设置DataContext

        DataContext="{Binding RelativeSource={RelativeSource Self}}"

我没有得到任何数据。

这是XAML,在我将DataContext从代码隐藏中删除后,我在其中设置它:

<Window x:Class="ItemsControlDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ItemsControlDemo"
        Title="Items Control Demo" Height="350" Width="400"
        WindowStartupLocation="CenterScreen"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <DataGrid Grid.Row="0" Name="TheDataGrid" SelectedValuePath="ID"
                  AutoGenerateColumns="False"
                  AlternatingRowBackground="Bisque"
                  ItemsSource="{Binding PersonList}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID"         Binding="{Binding Path=ID}"/>
                <DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"/>
                <DataGridTextColumn Header="Last Name"  Binding="{Binding Path=LastName}"/>
            </DataGrid.Columns>
        </DataGrid>
        <StackPanel Grid.Row="1" Orientation="Horizontal"
                    HorizontalAlignment="Left" VerticalAlignment="Bottom">
            <Button Content="Add Item"    Margin="5" Click="Button_Add_Click"/>
            <Button Content="Remove Item" Margin="5" Click="Button_Remove_Click"/>
        </StackPanel>
    </Grid>
</Window>

如果能对我的错误行为提供任何帮助,我们将不胜感激。

谢谢!

在XAML中而不是在代码隐藏中设置DataContext

在调用InitializeComponent 之前,您需要设置源

编辑:实际上,您只需要在InitializeComponent之前实例化它,因为它是一个ObservableCollection,它实现了INotifyCollectionChanged,所以如果您修改集合,您的网格将随更改而更新。

public MainWindow()
    {
        PersonList = new ObservableCollection<Person>();
        InitializeComponent();
        PersonList.Add(new Person(16, "Abraham", "Lincoln"));
        PersonList.Add(new Person(32, "Franklin", "Roosevelt"));
        PersonList.Add(new Person(35, "John", "Kennedy"));
        PersonList.Add(new Person(2, "John", "Adams"));
        PersonList.Add(new Person(1, "George", "Washington"));
        PersonList.Add(new Person(7, "Andrew", "Jackson"));
    }

216的答案有效。或者,您可以实现INotifyPropertyChanged,以便在设置PersonList时通知XAML。

ObservableCollection仅在集合中的项发生更改时发出通知。如果要在为集合分配新值时发出通知(例如PersonList = new ObservableCollection<Person>()),则需要实现INotifyPropertyChanged

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private ObservableCollection<Person> personList;
    public ObservableCollection<Person> PersonList
    {
        get { return personList; }
        set
        {
            if (value == personList)
                return;
            personList = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("PersonList"));
        }
    }
    public MainWindow()
    {
        InitializeComponent();
        PersonList = new ObservableCollection<Person>();
        // etc.
    }
}