首页 > 代码库 > D22_06_DataGrid控件
D22_06_DataGrid控件
<Window x:Class="demo.DataGridTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="DataGridTest" Height="300" Width="300" xmlns:local="clr-namespace:demo"> <Window.Resources> <local:ImagePathConverter x:Key="ImagePathConverter"></local:ImagePathConverter> </Window.Resources> <Grid> <!--FrozenColumnCount冻结列,表示这些列不能随滚动条的滚动而滚动--> <DataGrid x:Name="gridProducts" AutoGenerateColumns="False" FrozenColumnCount="2" > <DataGrid.Columns> <DataGridTextColumn Header="Product" Width="175" Binding="{Binding ModelName}"></DataGridTextColumn> <DataGridTextColumn Header="Price" Binding="{Binding UnitCost,StringFormat={}{0:C}}"></DataGridTextColumn> <DataGridTextColumn Header="Model Number" Binding="{Binding ModelNumber}"></DataGridTextColumn> <!--多文本换行要用ElementStyle--> <DataGridTextColumn Width="400" Binding="{Binding Description}" Header="Description"> <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="TextWrapping" Value="Wrap"></Setter> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <DataGridTextColumn IsReadOnly="True" Header="Category" Binding="{Binding CategoryName}"></DataGridTextColumn> <!--显示图片需要用DataGridTemplateColumn--> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Image Stretch="None" Source="{Binding ProductImagePath,Converter={StaticResource ImagePathConverter}}"></Image> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid></Window>
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;namespace demo{ /// <summary> /// DataGridTest.xaml 的交互逻辑 /// </summary> public partial class DataGridTest : Window { public DataGridTest() { InitializeComponent(); //绑定到ICollect(Product) gridProducts.ItemsSource = App.StoreDb.GetProducts(); } }}
D22_06_DataGrid控件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。