1

I'm having trouble binding a WPF DataGridComboBoxColumn and setting the initial value. I can populate the combo box with the data from a collection but I can't seem to set the initial value.

    <DataGridComboBoxColumn Header="Target Account Number" 
ItemsSource="{Binding Account.RawAccountNumber}"
                            SelectedValueBinding="{Binding Account.RawAccountNumber, Mode=TwoWay}"  
                            DisplayMemberPath="RawAccountNumber" 
                            SelectedValuePath="RawAccountNumber">
        <DataGridComboBoxColumn.ElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" 
                        Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Accounts}"/>
            </Style>
        </DataGridComboBoxColumn.ElementStyle>
        <DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" 
                        Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Accounts}"/>
            </Style>
        </DataGridComboBoxColumn.EditingElementStyle>
    </DataGridComboBoxColumn>

I've tried changing the ItemSource but this causes the combobox to be empty.

I've looked at several Stack Overflow posts but none of these have brought me to an answer yet. What can I try next?

Update

I intended to include my datagrid definition showing the Grid's ItemSource.

<DataGrid x:Name="dataGrid" DataGridCell.Selected="DataGrid_GotFocus" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="10,51,0,0" VerticalAlignment="Top" Height="521" Width="862" ItemsSource="{Binding Transaction.Rows}">
halfer
  • 19,824
  • 17
  • 99
  • 186
mack
  • 2,715
  • 8
  • 40
  • 68
  • Can you show us your ItemSource for the ComboboxColumn? – Daniele Sartori Nov 14 '17 at 15:36
  • It has been a long time since I did this, but I was thinking there was a property called `SelectedIndex` that will allow you to set the initial value according to the index of the value you wish to display. – Barns Nov 14 '17 at 15:41
  • I added the ItemSource, but it causes the combobox to not populate. – mack Nov 14 '17 at 15:45
  • @Barns so would I need to lookup the index value in the collection and set the SelectedIndex accordingly? – mack Nov 14 '17 at 15:46
  • Yes, you can do that in your code after the `DataGrid` is filled with data. – Barns Nov 14 '17 at 15:49
  • I don't understand your ItemsSource Bindings. You have `{Binding Account.RawAccountNumber}` on the DataGridComboBoxColumn (which looks wrong), and also bind it to `DataContext.Accounts` (of the parent Window) in the Styles. Does that make sense? And try not to listen to people telling you you should do this in code behind. – Clemens Nov 14 '17 at 15:58
  • What I have is a combobox that is filled with an ObservableCollection of Accounts. These are the values to pick from. The initial value for the combobox should be set to the Account.RawAccountNumber field of the datagrid's ItemSource. So the path to the value I'm trying to use as the initial combobox value is `Transaction.Row.Account.RawAccountNumber`. – mack Nov 14 '17 at 16:03
  • Binding `ItemsSource="{Binding Account.RawAccountNumber}"` makes no sense then. – Clemens Nov 14 '17 at 16:04
  • It doesn't make sense to set the ItemSource at all does it? – mack Nov 14 '17 at 16:09
  • If I throw this into a text column, the correct initial value is displayed `` – mack Nov 14 '17 at 16:11
  • What kind of items (type) does Account.RawAccountNumber returns? And what kind of items does Transaction.Rows returns? Please post the definitions of these classes. – mm8 Nov 15 '17 at 16:04

0 Answers0