如何在 Silverlight 中手动将数据添加到 DataGrid

发布于 2024-07-15 05:01:26 字数 367 浏览 7 评论 0原文

我发现数据网格列可以在 Silverlight 中动态创建和绑定。 但是我找不到将数据绑定到这些列的方法。

如果我尝试使用 AutoGenerateColumns = true 绑定任何类型的对象,则对象的每个属性的名称都会添加为列,并且除了不显示数据的现有列之外,对象信息也会显示在网格中。

如果我应用 AutoGenerateColumns = false 的列表,那么我仍然会在表中显示行,但列中没有数据。

我不想为需要在数据网格中显示数据的每种情况创建特定对象。

我不希望我的列名称仅限于属性名称,例如不带空格的名称。

我希望能够将列表或字典数组绑定到数据网格。 我还希望能够控制哪些数据显示在哪些列中。

I have found that datagrid columns can be dynamically created and bound in Silverlight. However I can't find a way to bind data to those columns.

If I try to bind any type of object with AutoGenerateColumns = true, then the names of each property of the object get added as columns and the object information is displayed in the grid in addition to the existing columns which show no data.

If I apply a list with AutoGenerateColumns = false, then i still get rows to show up in the table but no data in the columns.

I do not want to create a specific object for each case that i need to display data in the datagird.

I do not want to have my column names limited to the names of properties, e.g. names with out spaces.

I want to be able to bind a list or a dictionary array to the data grid. I also want to be able to control what data shows up in what columns.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是伱的 2024-07-22 05:01:26

假设您有一个类或一些其他容器想要将信息绑定到,您可以执行以下操作。

DataGridTextColumn product = new DataGridTextColumn();
                product.Binding = new System.Windows.Data.Binding("Product");
                product.Header = "Product";

                DataGridTextColumn date = new DataGridTextColumn();
                date.Binding = new System.Windows.Data.Binding("Date");
                date.Header = "Date";

                DataGridTextColumn version = new DataGridTextColumn();
                version.Binding = new System.Windows.Data.Binding("FileVersion");
                version.Header = "Version";

                DataGridTextColumn timestamp = new DataGridTextColumn();
                timestamp.Header = "TimeStamp";
                timestamp.Binding = new System.Windows.Data.Binding("TimeStamp");

                DataGridTextColumn user = new DataGridTextColumn();
                user.Header = "User";
                user.Binding = new System.Windows.Data.Binding("User");

                rpdata.Columns.Add(product);
                rpdata.Columns.Add(date);
                rpdata.Columns.Add(version);
                rpdata.Columns.Add(timestamp);
                rpdata.Columns.Add(user);

现在只需将数据网格的项目源设置为您拥有的项目集合。

这允许您设置要显示的标题,并将信息绑定到容器中的变量,从而仅显示您添加的列的信息。

Assume you have a class or some other container that you want to bind information to, you can do the following.

DataGridTextColumn product = new DataGridTextColumn();
                product.Binding = new System.Windows.Data.Binding("Product");
                product.Header = "Product";

                DataGridTextColumn date = new DataGridTextColumn();
                date.Binding = new System.Windows.Data.Binding("Date");
                date.Header = "Date";

                DataGridTextColumn version = new DataGridTextColumn();
                version.Binding = new System.Windows.Data.Binding("FileVersion");
                version.Header = "Version";

                DataGridTextColumn timestamp = new DataGridTextColumn();
                timestamp.Header = "TimeStamp";
                timestamp.Binding = new System.Windows.Data.Binding("TimeStamp");

                DataGridTextColumn user = new DataGridTextColumn();
                user.Header = "User";
                user.Binding = new System.Windows.Data.Binding("User");

                rpdata.Columns.Add(product);
                rpdata.Columns.Add(date);
                rpdata.Columns.Add(version);
                rpdata.Columns.Add(timestamp);
                rpdata.Columns.Add(user);

Now just set your itemsource of the datagrid to the collection of items you have.

This allows you to set the header you want to display, and it also binds the information to variables in the container, thus it displays the information of only the columns you add.

万劫不复 2024-07-22 05:01:26

您可能已经快到了,但只是在您的 xaml 中出现了一些微妙的错误,这是一个有效的示例:

<UserControl x:Class="Testproject.EditableDataGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid x:Name="myDataGrid" AutoGenerateColumns="False"> 
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header="My text" Binding="{Binding StringValue}" />
                <data:DataGridCheckBoxColumn Header="Check Box" Binding="{Binding IsChecked}" />
                <data:DataGridTemplateColumn Header="A template column">
                    <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Rectangle Width="20" Height="5" Fill="Red"/>
                            <TextBlock Text="{Binding IntValue}" />
                        </StackPanel>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>

            </data:DataGridTemplateColumn>
                <data:DataGridTextColumn Header="My int" Binding="{Binding IntValue}" />
            </data:DataGrid.Columns>
        </data:DataGrid>
    </Grid>
</UserControl>

using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;

namespace Testproject
{
    public partial class EditableDataGrid   :UserControl
    {
        public EditableDataGrid()
        {
            InitializeComponent();

            myDataGrid.ItemsSource = new List<ClassForDataGridTest>()
                            {
                                new ClassForDataGridTest() {StringValue="hello", IntValue=21, IsChecked=false}
                                , new ClassForDataGridTest() {StringValue="the second", IntValue=122, IsChecked = true}
                            };
        }
    }

    public class ClassForDataGridTest   : DependencyObject
    {

        public string StringValue {get; set;}

        public int IntValue {get; set;}

        public bool IsChecked { get; set; }

    }
}

You're probably almost there but just have something subtly wrong in your xaml, here's a working example:

<UserControl x:Class="Testproject.EditableDataGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid x:Name="myDataGrid" AutoGenerateColumns="False"> 
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header="My text" Binding="{Binding StringValue}" />
                <data:DataGridCheckBoxColumn Header="Check Box" Binding="{Binding IsChecked}" />
                <data:DataGridTemplateColumn Header="A template column">
                    <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Rectangle Width="20" Height="5" Fill="Red"/>
                            <TextBlock Text="{Binding IntValue}" />
                        </StackPanel>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>

            </data:DataGridTemplateColumn>
                <data:DataGridTextColumn Header="My int" Binding="{Binding IntValue}" />
            </data:DataGrid.Columns>
        </data:DataGrid>
    </Grid>
</UserControl>

using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;

namespace Testproject
{
    public partial class EditableDataGrid   :UserControl
    {
        public EditableDataGrid()
        {
            InitializeComponent();

            myDataGrid.ItemsSource = new List<ClassForDataGridTest>()
                            {
                                new ClassForDataGridTest() {StringValue="hello", IntValue=21, IsChecked=false}
                                , new ClassForDataGridTest() {StringValue="the second", IntValue=122, IsChecked = true}
                            };
        }
    }

    public class ClassForDataGridTest   : DependencyObject
    {

        public string StringValue {get; set;}

        public int IntValue {get; set;}

        public bool IsChecked { get; set; }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文