在设计时将数据绑定到 DataGrid
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
顺序如下:
1) 将代码从 MainWindow 类移至自定义类并设置
DataContext
属性:2) 更改
DataGrid
的绑定:3) 添加新的ResourceDictionary 的名称为
DataGridSample.xaml
,打开其属性(右键单击 -> Properties),将BuildAction
属性设置为 DesignData 并清除CustomTool
属性。4) 复制此代码,但将命名空间
vm
从WpfApplication1
更改为您的命名空间:5) 返回到
MainWindow.xaml
并添加下一个到Window
元素的行,其中所有声明如下:之后,您可以切换到设计模式,您将看到包含两行的数据网格。
The sequence is following:
1) Move code from the MainWindow class to a custom class and set the
DataContext
property:2) Change the binding of the
DataGrid
:3) Add new ResourceDictionary with the name
DataGridSample.xaml
, open its properties (Right Click -> Properties), set theBuildAction
property to the DesignData and clear the value of theCustomTool
property.4) Copy this code, but change the namespace
vm
from theWpfApplication1
to your one:5) Return to the
MainWindow.xaml
and add next lines to theWindow
element where all the declarations are:After that you can switch to the design mode and you will see the datagrid with two lines.