将数据绑定到网格

将数据绑定到网格

本文介绍了WPF8/C# - 将数据绑定到网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我发布了这个问题,但在接受了我最后一个问题的答案并阅读了这篇文章后,我意识到这不是我想要的答案.我再次发布了一些示例代码.

I understand I posted this question, but having accepted the answer on my last question and following through the article I realized it wasn't the answer I was looking for. I've posted again with some sample code.

我想用集合中的数据填充网格(不是 DataGrid).这是我所拥有的,但它不起作用.如果我删除集合并将 DataContext 设置为单个对象,它可以工作,但不能作为集合.

I want to fill a Grid (not a DataGrid) with Data from a collection. Here is what I have but it does not work. If I remove the collection and set the DataContext to a single object it works, but not as a collection.

XAML

Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <TextBlock Text="{Binding Path=StudentName}" />
        </StackPanel>
</Grid>

MainPage.xaml.cs

MainPage.xaml.cs

public MainPage()
    {
        InitializeComponent();

        ObservableCollection<Student> ob = new ObservableCollection<Student>();

        ob.Add(new Student()
        {
            StudentName = "James Jeffery"
        });

        ob.Add(new Student()
        {
            StudentName = "Sian Ellis"
        });



        this.DataContext = ob;

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }

这已经困扰了我好几个小时了.我似乎无法用集合填充网格.Google 上的每个示例都显示了 ListViews 等.我想填充一个网格,并且只填充一个网格.

This has been bugging me for hours. I just can't seem to fill a grid with a collection. Every example on Google shows ListViews etc. I want to fill a Grid, and only a Grid.

关于如何实现这一目标有什么建议吗?

Any advice on how to achieve this?

推荐答案

正如在另一个答案中提到的,您需要一个 ItemsControl:

As mentioned in another answer, you need an ItemsControl:

<Window x:Class="MiscSamples.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid IsItemsHost="True" Rows="3" Columns="3"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBox Text="{Binding Name}" Margin="2"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

背后的代码:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new List<Student>
                {
                    new Student() {Name = "James Jeffery"},
                    new Student() {Name = "Sian Ellis"},
                    new Student() {Name = "James Jeffery 2"},
                    new Student() {Name = "Sian Ellis 2"},
                    new Student() {Name = "James Jeffery 3"},
                    new Student() {Name = "Sian Ellis 3"},
                };
        }
    }

输出:

这篇关于WPF8/C# - 将数据绑定到网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:57