wpf 代码和 xaml 中的 ListBox.ItemsSource 绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3624496/
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-09-08 22:05:30  来源:igfitidea点击:

ListBox.ItemsSource binding in code and in xaml

wpflistboxitemssource

提问by Kirill Lykov

I wrote simple code like

我写了简单的代码,比如

public ObservableCollection<string> Names …
public Window1()
{
    PutInDataIntoNames();
    InitializeComponent();
    this.listBox1.ItemsSource = Names;
}

and in xaml

并在 xaml 中

<Grid>
    <ListBox Margin="10,11,10,16"
         Name="listBox1"
         Background="Black" 
         Foreground="Orange" 
         />
</Grid>

Then I wanted to set ItemsSource property in xaml. In order to do that I wrote the following:

然后我想在 xaml 中设置 ItemsSource 属性。为了做到这一点,我写了以下内容:

ItemsSource="{Binding Path=Names}"

Unfortunately, it doesn't work. Could you explain why and how to do that right?

不幸的是,它不起作用。你能解释一下为什么以及如何正确地做到这一点吗?

采纳答案by RockWorld

Do this in code behind

在后面的代码中执行此操作

public Window1() 
{ 
    PutInDataIntoNames(); 
    InitializeComponent(); 
    DataContext = this;
} 

and in XAML

并在 XAML 中

<Grid> 
    <ListBox ItemsSource="{Binding Names}"
         Margin="10,11,10,16" 
         Name="listBox1" 
         Background="Black"  
         Foreground="Orange"   
         /> 
</Grid>

Ideally you should follow MVVM design to isolate data from code behind.

理想情况下,您应该遵循 MVVM 设计将数据与背后的代码隔离。

回答by H.B.

If you only specify the binding path the binding engine will try to navigate the path starting from the current DataContextso ItemsSource="{Binding Path=Names}"does not work like this, there are a lot of different things to keep in mind especially when doing more complex things.

如果您只指定绑定路径,绑定引擎将尝试从当前路径开始导航,DataContext因此ItemsSource="{Binding Path=Names}"不会像这样工作,有很多不同的事情要记住,尤其是在做更复杂的事情时。

The single most important article that everyone who is new to DataBinding should read is the Data Binding Overview on MSDN

每个刚接触 DataBinding 的人都应该阅读的最重要的一篇文章是MSDN 上Data Binding 概述

To get back to your binding, if you want to do it completely in XAML you can do that as well, you just need to make the Window your source somehow, either by referencing it directly or relatively or by setting it up as the DataContext.

回到您的绑定,如果您想完全在 XAML 中完成它,您也可以这样做,您只需要以某种方式使 Window 成为您的源代码,通过直接或相对引用它或将其设置为 DataContext。

1 - Direct Reference:

1 - 直接参考:

<Window Name="Window"
        ...>
    <Grid> 
            <ListBox ...
                     ItemsSource="{Binding ElementName=Window, Path=Names}"
                     .../>
    </Grid>
</Window>

2 - Relative Reference

2 - 相对参考

    <Grid> 
            <ListBox ...
                     ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Names}"
                     .../>
    </Grid>

3 - Setting up the DataContext

3 - 设置数据上下文

<Window ...
        DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
    <Grid> 
            <ListBox ...
                     ItemsSource="{Binding Path=Names}"
                     .../>
    </Grid>
</Window>

回答by Viking

It seems that your Namesmight be a field. You can ONLY bind to public properties

看来你Names可能是一个领域。您只能绑定到公共属性