【问题标题】:Set DataContext in XAML Rather Than in Code Behind在 XAML 中而不是在代码后面设置 DataContext
【发布时间】:2014-10-05 19:55:49
【问题描述】:

我在其他帖子中读到,可以在 XAML 中指定,而不是在后面的代码中指定 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}}"

我没有得到任何数据。

这是我从后面的代码中删除 DataContext 后设置 DataContext 的 XAML:

<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>

对于我做错的事情的任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 有什么理由需要这样做,因为您没有通过在代码隐藏文件中放置大量代码来遵循 MVVM?
  • @Charleh:我知道我可以在后面的代码中做到这一点,但这是一个示例应用程序,我正在使用它来了解有关使用 DataGrid 绑定的更多信息。因此,我正在探索所有可能的方法。

标签: c# wpf xaml


【解决方案1】:

您需要调用 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"));
    }

【讨论】:

  • 谢谢!我没有意识到我在填充数据源之前正在初始化。非常感谢您的及时回答!
  • @PeterBuilt 另外,你的 Person 类需要实现 INotifyPropertyChanged
  • 由于我使用 ObservableCollection 作为我的 Person 对象列表,因此不需要显式实现 NotifyPropertyChange。我相信这是使用 ObservableCollection 的优势。
  • @PeterBuilt Well ObservableCollection 会通知您收藏的更改,但不会通知您的单个项目的更改,请参阅post
  • 有趣。我不知道这种区别。感谢您引起我的注意。
【解决方案2】:

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

ObservableCollection 仅在集合中的项目更改时通知。如果您想在为集合分配新值时通知(例如PersonList = new ObservableCollection&lt;Person&gt;()),则需要实现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.
    }
}

【讨论】:

  • 感谢您提供替代解决方案。
猜你喜欢
  • 2010-10-26
  • 2010-10-11
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多