wpf Datagrid列可以在不同的行中包含不同类型的控件吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14639292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:20:21  来源:igfitidea点击:

Can Datagrid column contain diffterent type of control in different row

c#.netwpfmvvm

提问by kidgu

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

在 Wpf 中,我可以创建一个数据网格,其中不同的行在同一列中包含不同类型的控件吗?

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

对于最简单的情况:具有 5 个列、2 行的数据网格,不关心第 5 个列中的第 4 个列:

  • 1st row: it's a textbox
  • 2nd row: it's a combobox.
  • 第一行:这是一个文本框
  • 第二行:它是一个组合框。

Thanks!

谢谢!

回答by Andrew

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

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

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:

这是一个将 DataGrid 绑定到(字符串)控件类型列表的演示应用程序。第一列只显示控件类型字符串,第二列作用于相同的信息来呈现对应的控件。您也许可以使 xaml 更简洁一些,但这是它的要点:

The XAML:

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-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" };
        }
    }
}

回答by Artless

Yes, you can. You need to create a template for your column.

是的你可以。您需要为您的列创建一个模板。

See this: WPF DataGrid different edit controls within a single column

请参阅:WPF DataGrid 单列中的不同编辑控件