WPF:在XAML中设置的ItemSource与code-背后 [英] WPF: Setting ItemSource in XAML vs. code-behind

查看:603
本文介绍了WPF:在XAML中设置的ItemSource与code-背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这是WPF中,它可能看起来像很多code,但不要被吓坏了,问题是很简单的!

Since this is WPF, it may look like lots of code, but don't be frightened, the question is really simple!

我有以下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>

我有这样的code-背后:

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!
        }
    }
}

如果我离开注释掉的语句 Hello.ItemSource = AllRoles ,网格显示的没有的。当我把它放回,它显示了正确的事情。这是为什么?

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?

推荐答案

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

表示绑定的ItemsSource 来的财产 this.DataContext.AllRoles ,其中是当前元素。

means "Bind ItemsSource to the property this.DataContext.AllRoles" where this is the current element.

Hello.ItemsSource = AllRoles;

表示绑定的ItemsSource 的ObservableCollection&LT; T&GT; 全角色,直接做你是什么想当初做的。

means "Bind ItemsSource to an ObservableCollection<T> full of roles", which directly does what you were trying to do originally.

有许多方法在XAML做到这一点。这里有一个:

There are a number of ways to do this in xaml. Here's one:

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窗口的公共属性。

OR, alternatively, you could make AllRoles a public property of the window

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告诉绑定走上逻辑树窗口

and then use a RelativeSource to tell the Binding to walk up the logical tree to the Window

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

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

Which means "Look at my ancestry until you find a Window, then look for a public property on the window called AllRoles".

但要做到这一点,最好的方法是完全跳过该死codebehind和使用 MVVM格局。如果你学习,你直接跳到MVVM模式我建议。学习曲线陡峭,但你了解所有关于WPF绑定和命令和重要的,很酷的事情。

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.

这篇关于WPF:在XAML中设置的ItemSource与code-背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆