none
WPF dataGrid样式 RRS feed

答案

  • 你好!

    根据你提供的图片和要求,我在本地创建了一个类似的工程。请查看下面的代码。

    XAML Code:

    <Window x:Class="WpfDataGridSample.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="Auto" Width="Auto">
        <Grid>
            <DataGrid Height="200" Width="Auto" HorizontalAlignment="Left" Margin="12,21,0,0" 
                Name="McDataGrid" VerticalAlignment="Top" RowHeight="30" ColumnWidth="100">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" Width="220"  Binding="{Binding Name}" >
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="TextWrapping" Value="Wrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                        <DataGridTextColumn.EditingElementStyle>
                            <Style TargetType="TextBox">
                                <Setter Property="Foreground" Value="Blue"/>
                            </Style>
                        </DataGridTextColumn.EditingElementStyle>
                    </DataGridTextColumn>
    
                    <DataGridTextColumn Header="Book Title"  Width="220"
                    Binding="{Binding BookTitle}" >
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="TextWrapping" Value="Wrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                        <DataGridTextColumn.EditingElementStyle>
                            <Style TargetType="TextBox">
                                <Setter Property="Foreground" Value="Blue"/>
                            </Style>
                        </DataGridTextColumn.EditingElementStyle>
                    </DataGridTextColumn>
    
                    <DataGridCheckBoxColumn   Header="MVP"  Width="50" Binding="{Binding IsMVP}"  IsThreeState="True" />
                    <DataGridTemplateColumn Header="DOB">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding DOB}" Margin="4"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <DatePicker SelectedDate="{Binding DOB, Mode=TwoWay}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    

    Code Behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    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.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfDataGridSample
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                McDataGrid.ItemsSource = LoadCollectionData();
            }
    
            /// List of Authors
            /// </summary>
            /// <returns></returns>
            private List<Author> LoadCollectionData()
            {
                List<Author> authors = new List<Author>();
                authors.Add(new Author()
                {
                    ID = 101,
                    Name = "Mahesh Chand",
                    BookTitle = "Graphics Programming with GDI+",
                    DOB = new DateTime(1975, 2, 23),
                    IsMVP = false
                });
                authors.Add(new Author()
                {
                    ID = 201,
                    Name = "Mike Gold",
                    BookTitle = "Programming C#",
                    DOB = new DateTime(1982, 4, 12),
                    IsMVP = true
                });
                authors.Add(new Author()
                {
                    ID = 244,
                    Name = "Mathew Cochran",
                    BookTitle = "LINQ in Vista",
                    DOB = new DateTime(1985, 9, 11),
                    IsMVP = true
                });
                return authors;
            }
        }
    
    
        public class Author
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public DateTime DOB { get; set; }
            public string BookTitle { get; set; }
            public bool IsMVP { get; set; }
        }
    }
    

    程序运行的截图,

    我将这个程序的源代码上传至SkyDrive上了,你可以从下面的地址下载,

    https://skydrive.live.com/redir?resid=F6BFF8BD172F0399!172

    推荐一些关于Data Grid的资料给你吧,

    #Data Grid in WPF

    http://www.c-sharpcorner.com/uploadfile/mahesh/datagrid-in-wpf/

    #WPF Data Grid Control

    http://www.wpftutorial.net/DataGrid.html#rowDetails

    谢谢!


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年10月1日 2:43
    版主