C# WPF:在XAML中设置ItemSource与代码隐藏

C# WPF:在XAML中设置ItemSource与代码隐藏,c#,wpf,listview,itemssource,C#,Wpf,Listview,Itemssource,由于这是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:N

由于这是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!
        }
    }
}
使用System.Collections.ObjectModel;
使用System.Windows;
名称空间hax
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
public observeCollection AllRoles{get{return m_AllRoles;}set{m_AllRoles=value;}}
私有ObservableCollection m_AllRoles=新ObservableCollection();
公共主窗口()
{
this.InitializeComponent();
添加(新角色(“约翰”、“经理”);
添加(新角色(“安妮”、“受训者”);
//Hello.ItemsSource=AllRoles;//注意这个!
}
}
}
如果我将语句
Hello.ItemSource=AllRoles
注释掉,网格将不显示任何内容。当我把它放回去时,它会显示正确的东西。这是为什么?

这是:

<ListView ItemsSource="{Binding AllRoles}" Name="Hello">
表示“将
itemsource
绑定到一个
observedcollection
充满角色的集合”,该集合直接执行您最初尝试执行的操作

在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;
    }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
this.InitializeComponent();
var allRoles=新的ObservableCollection()
添加(新角色(“约翰”、“经理”);
添加(新角色(“安妮”、“受训者”);
this.DataContext=allRoles;
}
}
在xaml中

<ListView ItemsSource="{Binding}" Name="Hello">

或者,您可以将所有角色设置为窗口的公共属性

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;
    }
}
公共部分类主窗口:窗口
{
公共ObservableCollection AllRoles{get;private set;}
公共主窗口()
{
this.InitializeComponent();
var allRoles=新的ObservableCollection()
添加(新角色(“约翰”、“经理”);
添加(新角色(“安妮”、“受训者”);
this.AllRoles=所有角色;
}
}
然后使用RelativeSource告诉绑定沿着逻辑树走到窗口

<ListView 
  ItemsSource="{Binding AllRoles, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" 
  Name="Hello">

这意味着“查看我的祖先,直到找到一个窗口,然后在窗口上查找名为AllRoles的公共属性”


但最好的方法是完全跳过该死的代码隐藏,如果您正在学习直接跳到MVVM模式,请使用我的建议。学习曲线是陡峭的,但您学习了所有关于绑定和命令以及关于WPF的重要而酷的东西。

当您在WPF中绑定到一个数据源时,它正在寻找窗口数据上下文的一个属性,称为“AllRoles”。有关xaml中数据绑定的更多信息,请查看模型-视图-模型模式。

<ListView 
  ItemsSource="{Binding AllRoles, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" 
  Name="Hello">