C# 在XAML中而不是在代码隐藏中设置DataContext
我在其他帖子中读到,可以在XAML中指定DataContext,而不是在其背后的代码中指定DataContext 我在我的主类的构造函数中声明并填充了一个ObservableCollection,在这里我还设置了DataContext:C# 在XAML中而不是在代码隐藏中设置DataContext,c#,wpf,xaml,C#,Wpf,Xaml,我在其他帖子中读到,可以在XAML中指定DataContext,而不是在其背后的代码中指定DataContext 我在我的主类的构造函数中声明并填充了一个ObservableCollection,在这里我还设置了DataContext: using System; using System.Windows; using System.Collections.ObjectModel; namespace ItemsControlDemo { public partial class Ma
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后设置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>
如果我做得不正确,任何帮助都将不胜感激
谢谢 在调用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"));
}
在调用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,则需要实现INotifyPropertyChanged
216的答案有效。或者,可以实现INotifyPropertyChanged,以便在设置PersonList时通知XAML ObservableCollection仅在集合中的项发生更改时通知。如果要在向集合分配新值时发出通知,例如PersonList=new ObservableCollection,则需要实现INotifyPropertyChanged
由于您没有通过在代码隐藏文件中加载代码来遵循MVVM,您有什么理由需要这样做吗?@Charleh:我知道我可以在代码隐藏中这样做,但这是一个示例应用程序,我正在使用它来了解有关使用带绑定的DataGrid的更多信息。因此,我正在探索所有可能的方法。您是否需要这样做,因为您没有通过在代码隐藏文件中加载代码来遵循MVVM?@Charleh:我知道我可以在代码隐藏中这样做,但这是一个示例应用程序,我正在使用它来了解有关使用带绑定的数据网格的更多信息。因此,我正在探索所有可能的方法来做到这一点。谢谢!在填充数据源之前,我没有意识到我正在初始化。非常感谢您的及时答复@PeterBuilt同样,Person类需要实现InotifyPropertyChanged,因为我对Person对象列表使用了ObservableCollection,所以我不需要显式实现NotifyPropertyChange。我相信这就是使用ObservaleCollection的优势。@PeterBuilt Well ObservaleCollection会通知您的集合中的更改,但不会通知您的单个项目中的更改,请看这个有趣的例子。我没有意识到这种区别。谢谢你引起我的注意,谢谢!在填充数据源之前,我没有意识到我正在初始化。非常感谢您的及时答复@PeterBuilt同样,Person类需要实现InotifyPropertyChanged,因为我对Person对象列表使用了ObservableCollection,所以我不需要显式实现NotifyPropertyChange。我相信这就是使用ObservaleCollection的优势。@PeterBuilt Well ObservaleCollection会通知您的集合中的更改,但不会通知您的单个项目中的更改,请看这个有趣的例子。我没有意识到这种区别。谢谢你提醒我。谢谢你提供了一个替代方案。谢谢你提供了一个替代方案。
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.
}
}