在设计时将数据绑定到 DataGrid

发布于 2024-11-02 01:49:50 字数 2123 浏览 0 评论 0原文

我正在学习 XAML,这是一个基本问题。
我想绑定数据,以便在设计时(在运行时它工作正常)通过 XAML 生成数据。

XAML

<Window x:Class="GridTest.MainWindow"
        Name="This"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False"
                  Height="200"
                  Width="308"
                  HorizontalAlignment="Left"
                  Margin="25,23,0,0"
                  Name="dataGrid1"
                  VerticalAlignment="Top"
                  ItemsSource="{Binding ElementName=This, Path=MyData}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="No"
                                    Width="Auto"
                                    Binding="{Binding Id}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Title"
                                    Binding="{Binding Title}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Description"
                                    Binding="{Binding Description}"
                                    IsReadOnly="True" />
            </DataGrid.Columns>
        </DataGrid>

代码隐藏

public partial class MainWindow : Window
{
    public List<Item> MyData
    {
        get
        {
            return myData;
        }

    }
    List<Item> myData;

    public MainWindow()
    {
        myData = new List<Item>
        {
            new Item{ Id=5, Description="Brown Car", Title="my car"},
            new Item{Id=1,Description="sweet dog", Title="my dog"},
        };
        InitializeComponent();
    }
}

public class Item
{
    public string Title
    {
        set;
        get;
    }
    public string Description
    {
        set;
        get;
    }
    public int Id
    {
        set;
        get;
    }
}

I am learning XAML and this is a basic question.
I want to bind data so it would be generated on design time (on runtime it works fine) via XAML.

XAML

<Window x:Class="GridTest.MainWindow"
        Name="This"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False"
                  Height="200"
                  Width="308"
                  HorizontalAlignment="Left"
                  Margin="25,23,0,0"
                  Name="dataGrid1"
                  VerticalAlignment="Top"
                  ItemsSource="{Binding ElementName=This, Path=MyData}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="No"
                                    Width="Auto"
                                    Binding="{Binding Id}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Title"
                                    Binding="{Binding Title}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Description"
                                    Binding="{Binding Description}"
                                    IsReadOnly="True" />
            </DataGrid.Columns>
        </DataGrid>

Code Behind

public partial class MainWindow : Window
{
    public List<Item> MyData
    {
        get
        {
            return myData;
        }

    }
    List<Item> myData;

    public MainWindow()
    {
        myData = new List<Item>
        {
            new Item{ Id=5, Description="Brown Car", Title="my car"},
            new Item{Id=1,Description="sweet dog", Title="my dog"},
        };
        InitializeComponent();
    }
}

public class Item
{
    public string Title
    {
        set;
        get;
    }
    public string Description
    {
        set;
        get;
    }
    public int Id
    {
        set;
        get;
    }
}

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

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

发布评论

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

评论(1

辞取 2024-11-09 01:49:50

顺序如下:

1) 将代码从 MainWindow 类移至自定义类并设置 DataContext 属性:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new MainWindowViewModel();
    }
}

public class MainWindowViewModel
{
    public MainWindowViewModel()
    {
        myData = new List<Item>
        {
            new Item{ Id=5, Description="Brown Car", Title="my car"},
            new Item{Id=1,Description="sweet dog", Title="my dog"},

        };
    }

    public List<Item> MyData
    {
        get
        {
            return myData;
        }

    }
    List<Item> myData;
}

2) 更改 DataGrid 的绑定:

ItemsSource="{Binding MyData}"

3) 添加新的ResourceDictionary 的名称为 DataGridSample.xaml,打开其属性(右键单击 -> Properties),将 BuildAction 属性设置为 DesignData 并清除CustomTool 属性。

4) 复制此代码,但将命名空间 vmWpfApplication1 更改为您的命名空间:

<vm:MainWindowViewModel xmlns:vm="clr-namespace:WpfApplication1">
    <vm:MainWindowViewModel.MyData>
        <vm:Item Id="1" Title="My dog" Description="Sweet dog" />
        <vm:Item Id="5" Title="My car" Description="Brown car" />
    </vm:MainWindowViewModel.MyData>
</vm:MainWindowViewModel>

5) 返回到 MainWindow.xaml 并添加下一个到 Window 元素的行,其中所有声明如下:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DataContext="{d:DesignData Source=DataGridSample.xaml}"

之后,您可以切换到设计模式,您将看到包含两行的数据网格。

The sequence is following:

1) Move code from the MainWindow class to a custom class and set the DataContext property:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new MainWindowViewModel();
    }
}

public class MainWindowViewModel
{
    public MainWindowViewModel()
    {
        myData = new List<Item>
        {
            new Item{ Id=5, Description="Brown Car", Title="my car"},
            new Item{Id=1,Description="sweet dog", Title="my dog"},

        };
    }

    public List<Item> MyData
    {
        get
        {
            return myData;
        }

    }
    List<Item> myData;
}

2) Change the binding of the DataGrid:

ItemsSource="{Binding MyData}"

3) Add new ResourceDictionary with the name DataGridSample.xaml, open its properties (Right Click -> Properties), set the BuildAction property to the DesignData and clear the value of the CustomTool property.

4) Copy this code, but change the namespace vm from the WpfApplication1 to your one:

<vm:MainWindowViewModel xmlns:vm="clr-namespace:WpfApplication1">
    <vm:MainWindowViewModel.MyData>
        <vm:Item Id="1" Title="My dog" Description="Sweet dog" />
        <vm:Item Id="5" Title="My car" Description="Brown car" />
    </vm:MainWindowViewModel.MyData>
</vm:MainWindowViewModel>

5) Return to the MainWindow.xaml and add next lines to the Window element where all the declarations are:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DataContext="{d:DesignData Source=DataGridSample.xaml}"

After that you can switch to the design mode and you will see the datagrid with two lines.

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