开发者

Binding data to a DataGrid on design time

开发者 https://www.devze.com 2023-02-27 02:15 出处:网络
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.

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"
                                    开发者_JAVA百科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;
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消