5
votes

I have a dynamically created DataGrid with the following XAML:

 <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Cells}" IsReadOnly="True" Visibility="{Binding VisibilityStatus}"       
                                  CanUserResizeColumns="False" CanUserAddRows="False" RowBackground="{Binding Color}" Margin="0" RowHeight="25" HeadersVisibility="All">
                                <DataGrid.Columns>
                                    <DataGridTemplateColumn Header="{Binding Path=Header}"> 
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <ComboBox ItemsSource="{Binding UniqueValuesToLapMapping.Keys, UpdateSourceTrigger=PropertyChanged}" 
                                                          SelectedItem="{Binding SelectedValue, Mode=TwoWay}"
                                                          SelectedIndex="0" Width="55"
                                                          Background="{Binding Color}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                </DataGrid.Columns>
                            </DataGrid>

The table is being displayed correctly with all of its content except for the header. No matter in which class I put the Header Property I can't seem to bind to it. I've tried putting it into the ViewModel, putting it into the class that has the "Cells" property, and putting it into the class that has all the Properties that I bind to in the ComboBox. I have also tried binding without the "Path=" and setting the UpdateSourceTrigger to OnPropertyChanged and changing the properties value on the runtime: nothing seems to work. What am I missing here?

This is the Property I am binding to that I've put in all these classes:

    private string _header = "TEST";
    public string Header
    {
        get { return _header; }
        set { SetField(ref _header, value, "Header"); }
    }
1

1 Answers

6
votes

It turns out that the was not in any logical or visual tree. This solution worked for me:

http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/