C# WPF:在 XAML 中设置 ItemSource 与代码隐藏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2147559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: Setting ItemSource in XAML vs. code-behind
提问by Deniz Dogan
Since this is WPF, it may look like lots of code, but don't be frightened, the question is really simple!
既然是WPF,看起来代码很多,不过别怕,问题其实很简单!
I have the following XAML:
我有以下 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>
I have this code-behind:
我有这个代码隐藏:
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!
}
}
}
If I leave the statement Hello.ItemSource = AllRoles
commented out, the grid displays nothing. When I put it back in, it displays the correct thing. Why is this?
如果我将语句Hello.ItemSource = AllRoles
注释掉,网格将不显示任何内容。当我把它放回去时,它显示了正确的东西。为什么是这样?
采纳答案by Deniz Dogan
This:
这个:
<ListView ItemsSource="{Binding AllRoles}" Name="Hello">
means "Bind ItemsSource
to the property this.DataContext.AllRoles
" where this
is the current element.
表示“绑定ItemsSource
到属性this.DataContext.AllRoles
”,其中this
是当前元素。
Hello.ItemsSource = AllRoles;
means "Bind ItemsSource
to an ObservableCollection<T>
full of roles", which directly does what you were trying to do originally.
意思是“绑定ItemsSource
到一个ObservableCollection<T>
完整的角色”,它直接做你最初想做的事情。
There are a number of ways to do this in xaml. Here's one:
在 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;
}
}
and in the xaml
并在 xaml 中
<ListView ItemsSource="{Binding}" Name="Hello">
OR, alternatively,you could make AllRoles a public property of the window
或者,或者,您可以将 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;
}
}
and then use a RelativeSource to tell the Binding to walk up the logical tree to the Window
然后使用 RelativeSource 告诉 Binding 沿着逻辑树向上走到 Window
<ListView
ItemsSource="{Binding AllRoles, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
Name="Hello">
Which means "Look at my ancestry until you find a Window, then look for a public property on the window called AllRoles".
这意味着“查看我的祖先直到找到一个 Window,然后在该窗口上查找名为 AllRoles 的公共属性”。
But the best way to do this is to skip the frigging codebehind altogether and use the MVVM pattern.I'd suggest if you're learning that you skip directly to the MVVM pattern. The learning curve is steep, but you learn all about binding and commands and the important, cool things about WPF.
但最好的方法是完全跳过该死的代码隐藏并使用MVVM 模式。如果您正在学习,我建议您直接跳到 MVVM 模式。学习曲线很陡峭,但您可以学习所有关于绑定和命令的知识,以及有关 WPF 的重要而酷的东西。
回答by Steve Danner
When you bind to a datasource in WPF, it is looking for a property of your Window's data context called "AllRoles". Check out the Model-View-ViewModel pattern for more on data binding in xaml. http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
当您绑定到 WPF 中的数据源时,它会查找名为“AllRoles”的 Window 数据上下文的属性。查看 Model-View-ViewModel 模式以了解有关 xaml 中数据绑定的更多信息。 http://msdn.microsoft.com/en-us/magazine/dd419663.aspx