C# 如何将 ItemsControl.ItemsSource 与 XAML 中的属性绑定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16694327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:41:39  来源:igfitidea点击:

How can I bind an ItemsControl.ItemsSource with a property in XAML?

c#wpfbindingwpf-controls

提问by Jonny Piazzi

I have a simple window :

我有一个简单的窗口:

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

And the associated code behind :

以及背后的相关代码:

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

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

    // Other code ...
}

If I write the following binding in the window constructor :

如果我在窗口构造函数中编写以下绑定:

lvItems.ItemsSource = Activities;

then my ListView is automatically updated when I add or remove elements from Activities.

那么当我添加或删除元素时,我的 ListView 会自动更新Activities

How should I write the binding in XAML?
I tried this but it doesn't work:

我应该如何在 XAML 中编写绑定?
我试过这个,但它不起作用:

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

How do I make this work in XAML?

我如何在 XAML 中完成这项工作?

采纳答案by sexta13

What @JesseJames says is true but not enough.

@JesseJames 说的是真的,但还不够。

You have to put

你必须把

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

as

作为

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

And the binding should be:

绑定应该是:

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

Regards,

问候,

回答by opewix

Set DataContext = thisin the Window constructor.

DataContext = this在 Window 构造函数中设置。

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

Then you will be able to bind Activities as you want: <ListView x:Name="lvItems" ItemsSource="{Binding=Activities}" />

然后,您将能够根据需要绑定活动: <ListView x:Name="lvItems" ItemsSource="{Binding=Activities}" />

回答by devdigital

That's because the data context of your view hasn't been set. You could either do this in the code behind:

这是因为您的视图的数据上下文尚未设置。您可以在后面的代码中执行此操作:

this.DataContext = this;

Alternatively, you could set the Window's DataContextto itself - DataContext="{Binding RelativeSource={RelativeSource Self}}"

或者,您可以将窗口设置DataContext为自身 -DataContext="{Binding RelativeSource={RelativeSource Self}}"

You're much better off though investigating the MVVM design pattern, and using an MVVM framework.

通过研究MVVM 设计模式使用 MVVM 框架,您会好得多。

回答by Farhad Jabiyev

You must set DataContext to thislike others answered, but you can set DataContextthrough xamlalso:

您必须设置的DataContext到这个像人回答了,但您可以设置DataContext通过xaml也:

<Window x:Class="WinActivityManager"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <ListView x:Name="lvItems" ItemsSource="{Binding Path=Activities}" />
    </Grid>
</Window>