如何绑定在WPF窗口的财产ItemsControl.ItemsSource? [英] How to Bind an ItemsControl.ItemsSource with a property of the window in WPF?

查看:245
本文介绍了如何绑定在WPF窗口的财产ItemsControl.ItemsSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个窗口是这样的:

Hi I have a Window like this:

<Window x:Class="WinActivityManager"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <ListView x:Name="lvItems" />
    </Grid>
</Window>

0的.cs是这样的。

O .cs is like this

public partial class WinActivityManager : Window
{
    private ObservableCollection<Activity> Activities { get; set; }

    public WinActivityManager()
    {
        Activities = new ObservableCollection<Activity>();
        InitializeComponent();
    }

    // Other code ...
}

如果我写的构造是这样的:

If I write in constructor something like this:

lvItems.ItemsSource = Activities;

当我添加或删除活动元素我的ListView被自动更新。结果
好。但如何我写在XAML本装订线?结果
我尝试这一点,但不工作:

When I add or remove elements from Activities my ListView is automatically update.
Ok. But How I wrote this binding line in XAML?
I try this but don't work:

<ListView x:Name="lvItems" ItemsSource="{Binding=Activities}" />

我怎么能在XAML中做这个工作?

How I can do this work in XAML?

推荐答案

什么@JesseJames说的是真实的,但还远远不够。

What @JesseJames says is true but not enough.

您必须把

private ObservableCollection<Activity> Activities { get; set; } 

public ObservableCollection<Activity> Activities { get; set; }

和绑定应该是:

<ListView x:Name="lvItems" ItemsSource="{Binding Path=Activities}" />

问候,

这篇关于如何绑定在WPF窗口的财产ItemsControl.ItemsSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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