none
How to Displaying ComboBox for all the columns in a specific row of a DataGrid RRS feed

  • Question

  • Hello All

    I need some help on Displaying ComboBox for all the columns in a specific row of a DataGrid and in another row the cell type should be a textbox  and should show a button on focus to the cell.

    Regards,

    Srinivas

    Friday, April 12, 2019 7:45 PM

Answers

  • Hi Srinivas,
    to set columns programmatically I show in the following demo. But it's not clear how looks the data model and the binding.

    XAML:

    <Window x:Class="WpfApp1.Window24"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="Window24" Height="450" Width="800">
      <Window.Resources>
        <local:Window24VM x:Key="vm"/>
      </Window.Resources>
      <Grid DataContext="{StaticResource vm}">
        <DataGrid Name="dg" ItemsSource="{Binding ParameterTypes}" 
                  AutoGenerateColumns="False" 
                  ColumnHeaderHeight="50">
          <DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
              <Setter Property="ContentTemplate">
                <Setter.Value>
                  <DataTemplate>
                    <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
                  </DataTemplate>
                </Setter.Value>
              </Setter>
            </Style>
          </DataGrid.ColumnHeaderStyle>
          <DataGrid.Columns>
            <DataGridTextColumn Header="Element" Binding="{Binding}"/>
          </DataGrid.Columns>
        </DataGrid>
      </Grid>
    </Window>
    

    the code:

    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace WpfApp1
    {
      /// <summary>
      /// Interaction logic for Window24.xaml
      /// </summary>
      public partial class Window24 : Window
      {
        public Window24()
        {
          InitializeComponent();
          AddDynamicColumns();
        }
    
        private List<string> columnList = new List<string>()
        { "Upstream End",
          "End of Segment 1",
          "Begining of Segment 2",
          "End OF Segment 2",
          "Begining OF Segment 3",
          "Downstream End" };
    
        private void AddDynamicColumns()
        {
          foreach (string col in columnList)
          {
            // variables
            DataTemplate dtemp;
            DataTrigger dtrig;
            FrameworkElementFactory fef;
            Style st;
    
            // ContentControl with Style
            fef = new FrameworkElementFactory(typeof(ContentControl));
            st = new Style(typeof(ContentControl));
            fef.SetValue(ContentControl.StyleProperty, st);
    
            // Column with DataTemplate
            DataGridTemplateColumn newCol = new DataGridTemplateColumn() { Header = col, Width = 80 };
            dtemp = new DataTemplate();
            dtemp.VisualTree = fef;
            newCol.CellTemplate = dtemp;
            dg.Columns.Add(newCol);
    
            // Triggers
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Pipe Type" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(ComboBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
    
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Pipe Material" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(ComboBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
    
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Distance" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(TextBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
    
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Width" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(TextBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
    
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Height" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(TextBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
          }
        }
      }
      public class Window24VM
      {
        public Window24VM()
        {
          ParameterTypes = new ObservableCollection<string>() { "Pipe Type", "Pipe Material", "Distance", "Width", "Height" };
        }
        public ObservableCollection<string> ParameterTypes
        {
          get;
          private set;
        }
    
      }
    }
    


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, April 16, 2019 4:41 AM

All replies

  • Hi Srinivas,
    here is a demo für a DataGridComboBoxColumn with more columns in list:

    XAML:

    <Window x:Class="Window17"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="Window17" Height="450" Width="800">
      <Window.Resources>
        <local:Window17VM x:Key="vm"/>
        <local:Window17Conv x:Key="conv"/>
      </Window.Resources>
      <Grid DataContext="{StaticResource vm}">
        <DataGrid ItemsSource="{Binding ViewPeople}" AutoGenerateColumns="False">
          <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTemplateColumn Header="Category">
              <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <Label>
                    <Label.Content>
                      <MultiBinding Converter="{StaticResource conv}">
                        <Binding Path="FK"/>
                        <Binding Path="ViewCategory" Source="{StaticResource vm}"/>
                      </MultiBinding>
                    </Label.Content>
                  </Label>
                </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
              <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                  <ComboBox ItemsSource="{Binding ViewCategory, Source={StaticResource vm}}"
                            SelectedValuePath="ID"
                            SelectedValue="{Binding FK, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
                    <ComboBox.ItemTemplate>
                      <DataTemplate>
                        <Grid>
                          <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50"/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                          </Grid.ColumnDefinitions>
                          <Label Grid.Column="0" Content="{Binding ID}"/>
                          <Label Grid.Column="1" Content="{Binding CategoryText}"/>
                          <Label Grid.Column="2" Content="{Binding Info}"/>
                        </Grid>
                      </DataTemplate>
                    </ComboBox.ItemTemplate>
                  </ComboBox>
                </DataTemplate>
              </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
          </DataGrid.Columns>
        </DataGrid>
      </Grid>
    </Window>

    And the classes:

    Imports System.ComponentModel
    Imports System.Data
    Imports System.Globalization
    
    Public Class Window17VM
    
      Public Sub New()
        ds = GetDataSet()
        cvsPeople.Source = (From p In ds.Tables("People").AsEnumerable Select New People(p)).ToList
        cvsCategory.Source = (From c In ds.Tables("Category").AsEnumerable Select New Category(c)).ToList
      End Sub
    
      Private ds As DataSet
      Private cvsPeople As New CollectionViewSource
      Private cvsCategory As New CollectionViewSource
    
      Public ReadOnly Property ViewPeople As ICollectionView
        Get
          Return cvsPeople.View
        End Get
      End Property
    
      Public ReadOnly Property ViewCategory As ICollectionView
        Get
          Return cvsCategory.View
        End Get
      End Property
    
      Private Function GetDataSet() As DataSet
        ' simulate data loading
        Dim rnd As New Random ' for random lookup
        ' instatiate new Dataset
        Dim ds As New DataSet
        ' Lookup table
        Dim dtLookup As New DataTable("Category")
        With dtLookup
          With .Columns
            With .Add("ID", GetType(Integer))
              .AutoIncrement = True
              .AutoIncrementSeed = -1
              .AutoIncrementStep = -1
            End With
            .Add("CategoryText", GetType(String))
            .Add("Info", GetType(String))
          End With
          For i = 1 To 10
            .Rows.Add(Nothing, $"Category  {i}", $"Info {i}")
          Next
        End With
        ' Child
        Dim dt As New DataTable("People")
        With dt
          With .Columns
            .Add(New DataColumn() With {.ColumnName = "ID",
                       .DataType = GetType(Integer),
                       .AutoIncrement = True,
                       .AutoIncrementSeed = -1,
                       .AutoIncrementStep = -1,
                       .ColumnMapping = MappingType.Hidden})
            .Add("FK", GetType(Integer))
            .Add(New DataColumn() With {.ColumnName = "Name", .DataType = GetType(String)})
          End With
          For i = 1 To 20
            .Rows.Add(Nothing, -rnd.Next(1, 11), $"Name {i}")
          Next
        End With
        ' Dataset and Relation
        With ds
          With .Tables
            .Add(dtLookup)
            .Add(dt)
          End With
          .Relations.Add("Rel", dtLookup.Columns("ID"), dt.Columns("FK"))
        End With
        Return ds
      End Function
    
      Public Class Category
        Public Sub New(row As DataRow)
          Me.r = row
        End Sub
        Private r As DataRow
    
        Public Property ID As Integer
          Get
            Return r.Field(Of Integer)("ID")
          End Get
          Set(value As Integer)
            r.SetField(Of Integer)("ID", value)
          End Set
        End Property
    
        Public Property CategoryText As String
          Get
            Return r.Field(Of String)("CategoryText")
          End Get
          Set(value As String)
            r.SetField(Of String)("CategoryText", value)
          End Set
        End Property
    
        Public Property Info As String
          Get
            Return r.Field(Of String)("Info")
          End Get
          Set(value As String)
            r.SetField(Of String)("Info", value)
          End Set
        End Property
      End Class
    
      Public Class People
        Public Sub New(row As DataRow)
          Me.r = row
        End Sub
        Private r As DataRow
    
        Public Property FK As Integer
          Get
            Return r.Field(Of Integer)("FK")
          End Get
          Set(value As Integer)
            r.SetField(Of Integer)("FK", value)
          End Set
        End Property
    
        Public Property Name As String
          Get
            Return r.Field(Of String)("Name")
          End Get
          Set(value As String)
            r.SetField(Of String)("Name", value)
          End Set
        End Property
      End Class
    
    End Class
    
    Public Class Window17Conv
      Implements IMultiValueConverter
    
      Public Function Convert(values() As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IMultiValueConverter.Convert
        Dim id As Integer? = CType(values(0), Integer?)
        Dim view = TryCast(values(1), ICollectionView)
        If id.HasValue AndAlso view IsNot Nothing Then _
          Return (From item In CType(view.SourceCollection, List(Of Window17VM.Category)) Where item.ID = id Select item.CategoryText).FirstOrDefault()
        Return Nothing
      End Function
    
      Public Function ConvertBack(value As Object, targetTypes() As Type, parameter As Object, culture As CultureInfo) As Object() Implements IMultiValueConverter.ConvertBack
        Throw New NotImplementedException()
      End Function
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, April 13, 2019 7:08 AM
  • Hello Peter,

    Thank you for the code sample provided but my requirement is is little different. I need to display "Combo Box" for the first 2 Rows of a Grid and normal cells for the remaining Rows.

    In your solution you are showing multiple columns in the Combobox's Dropdown.

    here is the code i got to display the first 2 rows to display ComboBox and came to know that the no of Columns also Dynamic. so can you please review the below code and suggest how to fix this?

    Regards,

    Srinivas

    Monday, April 15, 2019 1:35 PM
  • <Window x:Class="WpfApp1.GridExample"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="GridExample" Height="450" Width="650">
        <Grid>
            <DataGrid ItemsSource="{Binding ParameterTypes}" AutoGenerateColumns="False" ColumnHeaderHeight="50">
                <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGrid.ColumnHeaderStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Element" Binding="{Binding}"/>
                    <DataGridTemplateColumn Header="Upstream End" Width="70">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ContentControl>
                                    <ContentControl.Style>
                                        <Style TargetType="ContentControl">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding}" Value="Width">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Height">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Distance">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Type">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Material">
                                                <Setter Property="ContentTemplate">
                                                    <Setter.Value>
                                                        <DataTemplate>
                                                            <ComboBox />
                                                        </DataTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="End of Segment 1" Width="80">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ContentControl>
                                    <ContentControl.Style>
                                        <Style TargetType="ContentControl">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding}" Value="Width">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Height">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Distance">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Type">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Material">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Begining of Segment 2" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ContentControl>
                                    <ContentControl.Style>
                                        <Style TargetType="ContentControl">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding}" Value="Width">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Height">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Distance">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Type">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Material">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="End OF Segment 2" Width="80">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ContentControl>
                                    <ContentControl.Style>
                                        <Style TargetType="ContentControl">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding}" Value="Width">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Height">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Distance">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Type">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Material">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Begining OF Segment 3" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ContentControl>
                                    <ContentControl.Style>
                                        <Style TargetType="ContentControl">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding}" Value="Width">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Height">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Distance">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Type">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Material">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Downstream End" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ContentControl>
                                    <ContentControl.Style>
                                        <Style TargetType="ContentControl">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding}" Value="Width">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Height">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Distance">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                               <!--<TextBox />-->
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Type">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding}" Value="Pipe Material">
                                                    <Setter Property="ContentTemplate">
                                                        <Setter.Value>
                                                            <DataTemplate>
                                                                <ComboBox />
                                                            </DataTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    
    
    
    using System.Windows;
    using System.Collections.ObjectModel;
    
    namespace WpfApp1
    {
        /// <summary>
        /// Interaction logic for GridExample.xaml
        /// </summary>
        public partial class GridExample : Window
        {
            public GridExample()
            {
                InitializeComponent();
                this.DataContext = new GridLayoutViewModel();
            }
        }
    
        public class GridLayoutViewModel
        {
            public ObservableCollection<string> ParameterTypes
            {
                get;
                private set;
            }
            public GridLayoutViewModel()
            {
                ParameterTypes = new ObservableCollection<string>() {"Pipe Type", "Pipe Material", "Distance", "Width", "Height" };
            }
        }
    
    }

    Monday, April 15, 2019 1:38 PM
  • Hi,
    I didn't unterstand what do you want to fix.

    Do you want to know how to define the data model for binding all cells and lists in the ComboBoxes?

    Or do you want to know how to organize a variable number of columns with this structure (rows "Pipe Type" and "Pipe Materials" as Combobox, the others as TextBoxes)?


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Monday, April 15, 2019 3:28 PM
  • Hi Peter,

    I need to display the ComboBox for the rows of "Pipe Type" and "Pipe Materials" and the remaining with Text boxes. I am able achieve that for fixed no of Columns using XAML.
    but the columns in the grid are not fixed as they may increase or decrease based on the Segment number provided.
    so I am not sure how to make this without writing the XAML for each column.

    Alternately I am thinking of to show a Dropdown for each column of the Grid for those 2 rows on the Cell focus in edit mode.


    Regards,

    Srinivas

    Monday, April 15, 2019 5:16 PM
  • Hi Srinivas,
    to set columns programmatically I show in the following demo. But it's not clear how looks the data model and the binding.

    XAML:

    <Window x:Class="WpfApp1.Window24"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="Window24" Height="450" Width="800">
      <Window.Resources>
        <local:Window24VM x:Key="vm"/>
      </Window.Resources>
      <Grid DataContext="{StaticResource vm}">
        <DataGrid Name="dg" ItemsSource="{Binding ParameterTypes}" 
                  AutoGenerateColumns="False" 
                  ColumnHeaderHeight="50">
          <DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
              <Setter Property="ContentTemplate">
                <Setter.Value>
                  <DataTemplate>
                    <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
                  </DataTemplate>
                </Setter.Value>
              </Setter>
            </Style>
          </DataGrid.ColumnHeaderStyle>
          <DataGrid.Columns>
            <DataGridTextColumn Header="Element" Binding="{Binding}"/>
          </DataGrid.Columns>
        </DataGrid>
      </Grid>
    </Window>
    

    the code:

    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace WpfApp1
    {
      /// <summary>
      /// Interaction logic for Window24.xaml
      /// </summary>
      public partial class Window24 : Window
      {
        public Window24()
        {
          InitializeComponent();
          AddDynamicColumns();
        }
    
        private List<string> columnList = new List<string>()
        { "Upstream End",
          "End of Segment 1",
          "Begining of Segment 2",
          "End OF Segment 2",
          "Begining OF Segment 3",
          "Downstream End" };
    
        private void AddDynamicColumns()
        {
          foreach (string col in columnList)
          {
            // variables
            DataTemplate dtemp;
            DataTrigger dtrig;
            FrameworkElementFactory fef;
            Style st;
    
            // ContentControl with Style
            fef = new FrameworkElementFactory(typeof(ContentControl));
            st = new Style(typeof(ContentControl));
            fef.SetValue(ContentControl.StyleProperty, st);
    
            // Column with DataTemplate
            DataGridTemplateColumn newCol = new DataGridTemplateColumn() { Header = col, Width = 80 };
            dtemp = new DataTemplate();
            dtemp.VisualTree = fef;
            newCol.CellTemplate = dtemp;
            dg.Columns.Add(newCol);
    
            // Triggers
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Pipe Type" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(ComboBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
    
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Pipe Material" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(ComboBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
    
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Distance" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(TextBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
    
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Width" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(TextBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
    
            dtrig = new DataTrigger() { Binding = new Binding(), Value = "Height" };
            dtemp = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(TextBox));
            dtemp.VisualTree = fef;
            dtrig.Setters.Add(new Setter(ContentTemplateProperty, dtemp));
            st.Triggers.Add(dtrig);
          }
        }
      }
      public class Window24VM
      {
        public Window24VM()
        {
          ParameterTypes = new ObservableCollection<string>() { "Pipe Type", "Pipe Material", "Distance", "Width", "Height" };
        }
        public ObservableCollection<string> ParameterTypes
        {
          get;
          private set;
        }
    
      }
    }
    


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, April 16, 2019 4:41 AM
  • Hi Peter,

    Thank you for solution to display the Columns programmatically.
    We will have multiple Data models i.e. 1 for Data in the grid 2) Row parameters and still analyzing the same and the good thing is we don't need the Dropdowns.

    Also I need some help to display a button on focus to the cell for the Row ("Radius") and clicking that will display a modal screen to calculate radius based on the input parameters and return the value to that cell.
    attached the demo screenshot for your reference.

    Regards,
    Srinivas
    Tuesday, April 16, 2019 7:05 PM
  • Sorry unable to attach an image until msdn verify my account.
    Tuesday, April 16, 2019 7:09 PM