WPF:在 XAML 中设置 ItemSsource 与代码隐藏

2023-11-24

由于这是 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注释掉,网格显示nothing。当我把它放回去时,它会显示正确的内容。为什么是这样?


This:

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

意思是“绑定ItemsSource到酒店this.DataContext.AllRoles" where this是当前元素。

Hello.ItemsSource = AllRoles;

意思是“绑定ItemsSource to an 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 的重要、酷炫的内容。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF:在 XAML 中设置 ItemSsource 与代码隐藏 的相关文章

随机推荐