在设计时将数据绑定到DataGrid

时间:2011-04-17 11:37:40

标签: c# wpf xaml

我正在学习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;
    }
}

1 个答案:

答案 0 :(得分:4)

序列如下:

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)添加名为DataGridSample.xaml的新ResourceDictionary,打开其属性(右键单击 - &gt;属性),将BuildAction属性设置为DesignData并清除{{1}的值} property。

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

WpfApplication1

5)返回<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> 并将下一行添加到MainWindow.xaml元素,其中所有声明均为:

Window

之后你可以切换到设计模式,你会看到数据网格有两行。