Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Datagrid column contain diffterent type of control in different row

Tags:

c#

.net

mvvm

wpf

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

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

  • 1st row: it's a textbox
  • 2nd row: it's a combobox.

Thanks!

like image 272
kidgu Avatar asked Feb 01 '13 03:02

kidgu


People also ask

What is difference between DataGridView and DataGrid control in winforms?

The DataGrid control is limited to displaying data from an external data source. The DataGridView control, however, can display unbound data stored in the control, data from a bound data source, or bound and unbound data together.

What is the difference between grid and DataGrid in WPF?

A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.

What is the default event of DataGrid?

By default, the DataGrid control generates columns automatically when you set the ItemsSource property. The type of column that is generated depends on the type of data in the column.


1 Answers

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

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:

The 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" };
        }
    }
}
like image 61
Andrew Avatar answered Sep 24 '22 00:09

Andrew