[英]WPF: Setting ItemSource in XAML vs. code-behind
由于这是WPF,它看起来像很多代码,但是请不要害怕,问题确实很简单!
我有以下XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:hax="clr-namespace:hax" x:Class="hax.MainWindow"
x:Name="Window" Title="Haxalot" Width="640" Height="280">
<Grid x:Name="LayoutRoot">
<ListView ItemsSource="{Binding AllRoles}" Name="Hello">
<ListView.View>
<GridView>
<GridViewColumn Header="Name"
DisplayMemberBinding="{Binding Path=FullName}"/>
<GridViewColumn Header="Role"
DisplayMemberBinding="{Binding Path=RoleDescription}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
我有这个代码的背后:
using System.Collections.ObjectModel;
using System.Windows;
namespace hax
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<Role> AllRoles { get { return m_AllRoles; } set { m_AllRoles = value; } }
private ObservableCollection<Role> m_AllRoles = new ObservableCollection<Role>();
public MainWindow()
{
this.InitializeComponent();
AllRoles.Add(new Role("John", "Manager"));
AllRoles.Add(new Role("Anne", "Trainee"));
// Hello.ItemsSource = AllRoles; // NOTE THIS ONE!
}
}
}
如果我将注释Hello.ItemSource = AllRoles
保留为注释,则网格不会显示任何内容 。 当我放回去时,它显示正确的内容。 为什么是这样?
这个:
<ListView ItemsSource="{Binding AllRoles}" Name="Hello">
表示“将ItemsSource
绑定到属性this.DataContext.AllRoles
”,其中this
是当前元素。
Hello.ItemsSource = AllRoles;
表示“将ItemsSource
绑定到充满角色的ObservableCollection<T>
”,它直接执行您最初尝试执行的操作。
在xaml中有很多方法可以做到这一点。 这是一个:
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
var allRoles = new ObservableCollection<Role>()
allRoles.Add(new Role("John", "Manager"));
allRoles.Add(new Role("Anne", "Trainee"));
this.DataContext = allRoles;
}
}
并在xaml中
<ListView ItemsSource="{Binding}" Name="Hello">
或者,也可以将AllRoles设置为窗口的公共属性
public partial class MainWindow : Window
{
public ObservableCollection<Role> AllRoles {get;private set;}
public MainWindow()
{
this.InitializeComponent();
var allRoles = new ObservableCollection<Role>()
allRoles.Add(new Role("John", "Manager"));
allRoles.Add(new Role("Anne", "Trainee"));
this.AllRoles = allRoles;
}
}
然后使用RelativeSource告诉Binding将逻辑树向上移动到Window
<ListView
ItemsSource="{Binding AllRoles, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
Name="Hello">
这意味着“先查看我的祖先,直到找到一个窗口,然后在该窗口上查找名为AllRoles的公共属性”。
但是,执行此操作的最佳方法是完全跳过后面的限制代码,并使用MVVM模式。 我建议您是否正在学习直接跳至MVVM模式。 学习过程非常艰巨,但是您将学习有关绑定和命令的所有知识,以及有关WPF的重要且有趣的知识。
当您绑定到WPF中的数据源时,它正在寻找Window数据上下文的一个名为“ AllRoles”的属性。 请查看Model-View-ViewModel模式以获取有关xaml中数据绑定的更多信息。 http://msdn.microsoft.com/zh-CN/magazine/dd419663.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.