如何使用DataContext绑定到List ?

时间:2021-12-15 19:42:10

I'm self-learning C#, OOP, and WPF so the potential for stuff ups is staggering.

我正在自学C#,OOP和WPF,因此填充的可能性是惊人的。

So given that, can someone please explain why after clicking the button in my tiny test example the Name property appears in the TextBox but the ListBox shows nothing?

因此,有人可以解释为什么在我的小测试示例中单击按钮后,Name属性出现在TextBox中,但ListBox没有显示任何内容?

XAML:

<Window x:Class="BindingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BindingTest" Height="250" Width="300">
<Grid Name="mainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="50" />
        <RowDefinition Height="100" />
    </Grid.RowDefinitions>
    <Button 
            Grid.Row="0"
            Name="MakeIntListButton"
            Click="MakeIntListButton_Click">Make and Display Integer List</Button>
    <TextBox Grid.Row="1" Text ="{Binding Path=Name}"
             />
    <ListBox
        Grid.Row="2"
        ItemsSource="{Binding Path=MyIntegers}"
        />
</Grid>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BindingTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void MakeIntListButton_Click(object sender, RoutedEventArgs e)
        {
            AClass InstanceOfAClass = new AClass();
            InstanceOfAClass.MyIntegers.Add(6);
            InstanceOfAClass.MyIntegers.Add(7);
            InstanceOfAClass.MyIntegers.Add(42);

            InstanceOfAClass.Name = "Fred";


            mainGrid.DataContext =InstanceOfAClass ;
        }
    }

    public class AClass
    {
        public string Name {get;set;}
        public List<int> MyIntegers = new List<int>();
    }
}

3 个解决方案

#1


Part of me wonders whether it's something to do with the fact that "MyIntegers" is a public field rather than a property. Can you refactor you class to look like this and try it?

我的一部分想知道这是否与“MyIntegers”是公共领域而不是财产这一事实有关。你可以重构你的课程看起来像这样并试试吗?

public class AClass
{
    private List<int> _ints = new List<int>();

    public string Name { get; set; }
    public List<int> MyIntegers
    {
        get { return _ints; }
    }
}

#2


I ran your sample and when I clicked on the button the TextBox was populated with the Name as expected.

我运行了您的示例,当我单击按钮时,TextBox按预期填充了Name。

The only problem I encountered was that the ListView was not getting populated with the list of integers. That's to do with the fact that XAML is not very comfortable with generics if you modify it to bind to an array instead it works. WPF supports consumption of XAML fine, it's using generics within XAML that's not supported. As Matt Hamilton points out in his answer MyIntegers just needs to be made a propety by adding a get acessor.

我遇到的唯一问题是ListView没有填充整数列表。这与XAML对泛型不太相关的事实有关,如果你修改它以绑定到数组而不是它的工作原理。 WPF支持很好地使用XAML,它使用XAML中不支持的泛型。正如马特·汉密尔顿在他的回答中指出的那样,MyIntegers只需要通过增加一个获得者来成为一个普通人。

Add C# Property:

添加C#属性:

public int[] MyInts { get { return MyIntegers.ToArray(); } } 

XAML:

<ListBox Grid.Row="2" ItemsSource="{Binding Path=MyInts}" />

#3


Look into using the System.Collections.ObjectModel.ObservableCollection for list binding instead of a plain List.

查看使用System.Collections.ObjectModel.ObservableCollection进行列表绑定而不是普通List。

#1


Part of me wonders whether it's something to do with the fact that "MyIntegers" is a public field rather than a property. Can you refactor you class to look like this and try it?

我的一部分想知道这是否与“MyIntegers”是公共领域而不是财产这一事实有关。你可以重构你的课程看起来像这样并试试吗?

public class AClass
{
    private List<int> _ints = new List<int>();

    public string Name { get; set; }
    public List<int> MyIntegers
    {
        get { return _ints; }
    }
}

#2


I ran your sample and when I clicked on the button the TextBox was populated with the Name as expected.

我运行了您的示例,当我单击按钮时,TextBox按预期填充了Name。

The only problem I encountered was that the ListView was not getting populated with the list of integers. That's to do with the fact that XAML is not very comfortable with generics if you modify it to bind to an array instead it works. WPF supports consumption of XAML fine, it's using generics within XAML that's not supported. As Matt Hamilton points out in his answer MyIntegers just needs to be made a propety by adding a get acessor.

我遇到的唯一问题是ListView没有填充整数列表。这与XAML对泛型不太相关的事实有关,如果你修改它以绑定到数组而不是它的工作原理。 WPF支持很好地使用XAML,它使用XAML中不支持的泛型。正如马特·汉密尔顿在他的回答中指出的那样,MyIntegers只需要通过增加一个获得者来成为一个普通人。

Add C# Property:

添加C#属性:

public int[] MyInts { get { return MyIntegers.ToArray(); } } 

XAML:

<ListBox Grid.Row="2" ItemsSource="{Binding Path=MyInts}" />

#3


Look into using the System.Collections.ObjectModel.ObservableCollection for list binding instead of a plain List.

查看使用System.Collections.ObjectModel.ObservableCollection进行列表绑定而不是普通List。