1

I am trying to use a ComboBox in a DataGrid CellEditingTemplate, binding to an existing DataTable. When I double click the item, the ComboBox displays, but there is no data in it. I've researched different options for a couple of days, but nothing seems to work.

<DataGridTemplateColumn Header=" Venue" CanUserSort="False">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Venue}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox  ItemsSource="{Binding Path=dtVenues, ElementName=MyWindow}"
                       DisplayMemberPath="Venue" 
                       SelectedValuePath="Venue"
                       Text="{Binding Venue}"/> 
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

The DataTable dtVenues is declared as Public in code behind (VB). Can anyone please point me in the right direction for a solution.

Sheridan
  • 68,826
  • 24
  • 143
  • 183

1 Answers1

1

I'm guessing that you've declared the dtVenues collection for your ComboBoxes in the code behind of your Window and set the Window.DataContext property value to the code behind class in one way or another. If that is true, then I believe that you need to change your Binding slightly to address the Window.DataContext:

<ComboBox ItemsSource="{Binding Path=DataContext.dtVenues, ElementName=MyWindow}"... />
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Thanks for the suggestion. dtVenues is part of a DataSet and is declared and populated in MainWindow. The XAML is also for the MainWindow. Do I still need to set a DataContext? – user3061978 Dec 03 '13 at 17:54
  • `DataContext` in the `Binding.Path` above relates to whatever object you set as the `DataContext` of your `Window`. If you set that to itself (`MainWindow.xaml`), then you will need to use it in the `Path`. And yes, you *always* need to set the `DataContext` using one method or another, or the UI will have no... data context... no data. – Sheridan Dec 04 '13 at 09:03