Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView Combobox DataBinding WPF

I am facing some problems with databinding with Combobox inside of DataGrid in WPF XAML.

I have Signal Class which has ObservableCollection.

public struct RawVal
{
    public string name;  //Name of the Value
    public int value;    // Value
}


public class Signal
{
    public string Name { get; set; }
    public Int32 Value { get; set; }
    public ObservableCollection<RawVal> rawValue { get; set; }
};

And in Stettings window there are

public partial class Settings : Window
{      
  public ObservableCollection<Signal> tempSigList { get; set; }
  public ObservableCollection<RawVal> tempRawVal { get; set; }
  .........
  .........
this.grdSignal.ItemsSource = tempSigList;

}

And my XAML looks like this:

<DataGrid ItemsSource="{Binding}" Name="grdSignal">
 <DataGrid.Columns>    
    <DataGridTemplateColumn Header="   RAW Value ">
      <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
              <ComboBox ItemsSource="{Binding tempRawVal  }" SelectedItem="Binding name" />
           </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>

But no any results ㅠㅠㅠ I have tried many examples on stackoverflow but, no any results. Any suggestions or solutions are welcomed. Thank you

like image 907
Mamurbek Avatar asked Jun 26 '26 18:06

Mamurbek


1 Answers

Here:

Window:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    tempSigList = new ObservableCollection<Signal>();
    tempSigList.Add(new Signal { Name = "Name 1", Value = 1, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 1", value = 1 }, new RawVal { name = "combo 2", value = 2 } } });
    tempSigList.Add(new Signal { Name = "Name 2", Value = 2, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 3", value = 3 }, new RawVal { name = "combo 4", value = 4 } } });
    tempSigList.Add(new Signal { Name = "Name 3", Value = 3, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 5", value = 5 }, new RawVal { name = "combo 6", value = 6 } } });
    tempSigList.Add(new Signal { Name = "Name 4", Value = 4, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 7", value = 7 }, new RawVal { name = "combo 8", value = 8 } } });
    tempSigList.Add(new Signal { Name = "Name 5", Value = 5, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 9", value = 9 }, new RawVal { name = "combo 10", value = 10 } } });

    dataGrid1.ItemsSource = tempSigList;
}

XAML:

<DataGrid x:Name="dataGrid1" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="RAW Value">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding rawValue}"  SelectedItem="{Binding name}" DisplayMemberPath="name" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

enter image description here

I used AutoGenerateColumns=true. You can make it false and have only your ComboBox column to show up.

enter image description here

like image 193
jsanalytics Avatar answered Jun 29 '26 09:06

jsanalytics