WPF 数据网格样式

标签 wpf datagrid wpf-controls styles wpftoolkit

有没有人知道/有一个示例说明如何将 WPF DataGrid 布局更改为卡片 View 或其他任何内容,而不仅仅是行堆栈?

最佳答案

结果看起来像这样。 alt text http://iwebthereforeiam.com/files/ScreenShot.gif

这是应该演示这个想法的代码。

XAML:

<Window x:Class="StackOverflow_545979.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:StackOverflow_545979"
    xmlns:debug="clr-namespace:System.Diagnostics;assembly=System"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <local:GreekGods  x:Key="GreekGods"/>
        <DataTemplate x:Key="itemTemplate">
            <Border BorderBrush="RoyalBlue" BorderThickness="2" Margin="2" Padding="5">
                <WrapPanel Orientation="Vertical">
                    <TextBlock Width="200" Text="{Binding Path=Name}"/>
                    <TextBlock Width="200" Text="{Binding Path=Description}"/>
                    <TextBlock Width="200" Text="{Binding Path=RomanName}"/>
                </WrapPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>

    <ListBox ItemTemplate="{StaticResource itemTemplate}" 
             ItemsSource="{StaticResource GreekGods}" />
</Window>

C#代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace StackOverflow_545979
{
    public class GreekGod
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public string RomanName { get; set; }

        public GreekGod() { }
        public GreekGod(string name, string description, string romanName)
        {
            this.Name = name;
            this.Description = description;
            this.RomanName = romanName;
        }
    }

    public class GreekGods : ObservableCollection<GreekGod>
    {
        public GreekGods()
        {
            this.Add(new GreekGod("Aphrodite", "Goddess of love, beauty and fertility", "Venus"));
            this.Add(new GreekGod("Apollo", "God of prophesy, music and healing", "Apollo"));
            this.Add(new GreekGod("Ares", "God of war", "Mars"));
            this.Add(new GreekGod("Artemis", "Virgin goddess of the hunt", "Diana"));
            this.Add(new GreekGod("Athena", "Goddess of crafts and the domestic arts", "Athena"));
            this.Add(new GreekGod("Demeter", "Goddess of agriculture", "Ceres"));
            this.Add(new GreekGod("Dionysus", "God of wine", "Bacchus"));
            this.Add(new GreekGod("Hephaestus", "God of fire and crafts", "Vulcan"));
            this.Add(new GreekGod("Hera", "Goddess of marriage", "Juno"));
            this.Add(new GreekGod("Hermes", "Messenger of the Gods", "Mercury"));
            this.Add(new GreekGod("Poseidon", "God of the sea, earthquakes and horses", "Neptune"));
            this.Add(new GreekGod("Zeus", "Supreme God of the Olympians", "Jupiter"));
        }
    }
}

Bea Stollnitz's examples 解除的 GreekGod 和 GreekGods 类.

关于WPF 数据网格样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/545979/

相关文章:

wpf - 如何防止 TreeView 所选项目的子项继承其格式?

c# - 如何基于默认的 DataGrid 样式创建样式?

c# - 如何: Create a DockPanel in C# (WPF)

c# - 如何实例化复杂的控件组并访问它们的值? (wpf 与 Visual Studio )

wpf - MouseEnter 事件可以/应该冒泡吗?

c# - 使用 DrawingBrush 创建两条垂直线

c# - RadGridView 按钮列内容绑定(bind)到 DataMember

wpf - ControlTemplate 或 DataTemplate 中的自定义资源字典

c# - WPF DataGrid 行以编程方式双击事件

c# - 如何使 WPF StackPanel 适合网格单元格