0

I'm trying to learn WPF and I'm currently creating a grid and binding data to it. Currently text column data binds ok but when doing a combobox it displays no data in the list.

 public class MainWindowViewModel
    {
        public BindableCollection<feed> feeder { get; set; }
        public ObservableCollection<cboData> cboDatas { get; set; }
       
        public MainWindowViewModel()
        {
            feeder = new BindableCollection<feed>();


            feed feed1 = new feed();
            feed1.feedId = 1;
            feed1.name = "MDPT1";
            feed1.type = "type1";
            feed1.date = "03/02/1997";

            feeder.Add(feed1);

            feed feed2 = new feed();
            feed2.feedId = 2;
            feed2.name = "MDPT2";
            feed2.type = "type1";
            feed2.date = "03/04/1987";

            feeder.Add(feed2);


            cboDatas = new ObservableCollection<cboData>
                {
                    new cboData {id = 1, name = "DD 1"},
                    new cboData {id = 2, name = "DD 2"}
                };

        }


        public class cboData
        {
            public int id { get; set; }
            public string name { get; set; }
        }

    }
<DataGrid x:Name="feeder" HorizontalAlignment="Center" Height="106" Margin="0,302,0,0" VerticalAlignment="Top" Width="646" RenderTransformOrigin="0.5,0.5" AlternatingRowBackground="Gray" CanUserAddRows="False"
                  AutoGenerateColumns="False">
            <DataGrid.Columns>

                <DataGridTextColumn Header="Full Name"  Binding="{Binding Path=name}" />
                                
                <DataGridCheckBoxColumn Header="Tick" />

                <DataGridTemplateColumn Header="COMBO BOX">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox
                                    ItemsSource="{Binding Path=cboDatas, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
                                    DisplayMemberPath="name"/>
                                 <!--
                                    SelectedItem="{Binding Path= feedid, Mode=TwoWay}"
                                    SelectedValue="{Binding feeder}"
                                    SelectedValuePath="type"
                                -->                                
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>...

Can anyone see what i'm doing wrong with the bindings?

Also is it possible to dynamically create a grid column structure in code?

Thanks in advance for any help/pointers

MDP77
  • 61
  • 9
  • 2
    `ItemsSource="{Binding Path=DataContext.cboDatas, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"` – Clemens Nov 08 '21 at 18:35
  • Besides that, it is unclear what you are actually doing. There is apparently no Binding of the ItemsSource of the DataGrid, just a bunch of irrelevant property assignments. – Clemens Nov 08 '21 at 18:38
  • Thanks Clemens, DataContext sorted it - I'd been looking at it for ages and never noticed it was missing. What are the irrelevant property assignments? – MDP77 Nov 09 '21 at 10:06
  • All those property assignments on the DataGrid that are not relevant for your question. Why should we read all that to find out that there is no ItemsSource Binding? – Clemens Nov 09 '21 at 10:08
  • I get you now, I thought you meant they are not needed in general. As stated I'm trying to learn it so thought I'd cut it down to the minimum so the bigger picture could be seen. Thanks for your help. – MDP77 Nov 09 '21 at 10:23

0 Answers0