7

由于这是 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。当我把它放回去时,它会显示正确的东西。为什么是这样?

4

2 回答 2

16

这:

<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 的重要而酷的东西。

于 2010-01-27T14:29:45.653 回答
0

当您绑定到 WPF 中的数据源时,它正在寻找您的 Window 数据上下文的属性,称为“AllRoles”。查看 Model-View-ViewModel 模式,了解有关 xaml 中数据绑定的更多信息。 http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

于 2010-01-27T14:31:23.140 回答