可DataGrid列中包含不同的行diffterent类型的控件 [英] Can Datagrid column contain diffterent type of control in different row

查看:357
本文介绍了可DataGrid列中包含不同的行diffterent类型的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,我可以创造一个DataGrid,不同的行包含在同一列不同类型的控制?

In Wpf, Can I create a datagrid which different row contain different type of control in same column?

有关最简单的情况:数据网格与5 COLS,2行,不在乎4首COLS,在第5栏:

For easiest case: datagrid with 5 cols, 2 rows, don't care about 4 first cols, in 5th col:

  • 第1行:这是一个文本框
  • 第二行:这是一个组合框

谢谢!

推荐答案

您可以使用DataGridTemplateColumn结合一些触发器来实现这一功能。

You can use a DataGridTemplateColumn combined with a few triggers to achieve this functionality.

这是一个DataGrid结合的(串)控制类型列表中的演示应用程序。第一列只是显示控制类型字符串,和第二列作用于相同的信息到present相应控制。你也许可以使XAML有点更简洁,但是这是它的JIST:

This is a demo application that binds a DataGrid to a list of (string) Control Types. The first column just displays the control type string, and the second column acts on the same information to present the corresponding Control. You might be able to make the xaml a bit more concise, but this is the jist of it:

在XAML:

<Window x:Class="DataGridWithMultipleTypesPerColumn.MainWindow"
    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 ItemsSource="{Binding ControlTypes}"
              AutoGenerateColumns="False">
        <DataGrid.Columns>
        <DataGridTextColumn Header="Control Type" Binding="{Binding}"/>
            <DataGridTemplateColumn Header="Actual Control">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ContentControl>
                            <ContentControl.Style>
                                <Style TargetType="ContentControl">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding}" Value="TextBox">
                                            <Setter Property="ContentTemplate">
                                                <Setter.Value>
                                                    <DataTemplate>
                                                        <TextBox Text="Default Text"/>
                                                    </DataTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding}" Value="CheckBox">
                                            <Setter Property="ContentTemplate">
                                                <Setter.Value>
                                                    <DataTemplate>
                                                        <CheckBox Content="Check Box"/>
                                                    </DataTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding}" Value="Button">
                                            <Setter Property="ContentTemplate">
                                                <Setter.Value>
                                                    <DataTemplate>
                                                        <Button Content="Button"/>
                                                    </DataTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ContentControl.Style>
                        </ContentControl>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

code-后面视图模型:

Code-behind and View Model:

namespace DataGridWithMultipleTypesPerColumn
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }

    public class ViewModel
    {
        public ObservableCollection<string> ControlTypes
        {
            get;
            private set;
        }
        public ViewModel()
        {
            ControlTypes = new ObservableCollection<string>() { "Button", "TextBox", "CheckBox" };
        }
    }
}

这篇关于可DataGrid列中包含不同的行diffterent类型的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆