首页 > 代码库 > D22_06_DataGrid控件

D22_06_DataGrid控件

image

 

 

<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控件