James Cronen
James Cronen

Reputation: 5763

DataGridComboBoxes showing combobox choices but not text

My application has two WPF 4 DataGrids. The first allows a user to specify one or more configuration files. The second should have one column in which the user can choose an entry (chosen by name) from the first table.

I've elected to use a DataGridComboBoxColumn in the second table to list the rows from the first (and display them by name).

My I've implemented a WPF DataGrid as follows:

<DataGrid x:Name="TimePeriodDataGrid" Grid.RowSpan="2" Margin="0 5 5 0" 
          ItemsSource="{Binding TimePeriodCollection}" 
          SelectionChanged="TimePeriodDataGrid_SelectionChanged"
          AutoGenerateColumns="False"
          CanUserAddRows="False"
          CanUserDeleteRows="True"
          CanUserReorderColumns="False" 
          CanUserResizeColumns="True" 
          CanUserResizeRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Year" Binding="{Binding Year}"/>
        <DataGridTextColumn Header="Start Date" Binding="{Binding Path=Start, StringFormat={}{0:MMMM d}}" />
        <DataGridTextColumn Header="End Date" Binding="{Binding Path=End, StringFormat={}{0:MMMM d}}" />
        <DataGridComboBoxColumn Header="Configuration File" 
                                SelectedItemBinding="{Binding ConfigFile.Name}"
                                TextBinding="{Binding ConfigFile.Name}" 
            <DataGridComboBoxColumn.EditingElementStyle>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=ConfigFileCollection, Mode=OneWay}" />
                </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>

No text appears in the combobox columns when the combobox isn't active! I was under the impression that's the difference between the SelectedItemBinding and the TextBinding. However the SelectedItemBinding seems to be working perfectly (clicking on the combobox to activate it shows the proper value). And they have the exact same DataContext and Binding, so I'm not sure how one works and the other doesn't.

Additionally, there are no notifications in the console that indicate the TextBinding is erroring.

Is it related to the fact that I've declared an EditingElementStyle but not an ElementStyle? (And if it is, how do I set the TextBlockComboBox.Text since that appears to be protected by WPF?)

Any other suggestions on what I could be missing?

EDIT 17:23 EDT: I removed the TextBinding entry and I saw that the TextBinding is what's displayed when the Combobox is on screen but is "collapsed" without dropping down a list of options. This is displaying correctly.

My question is therefore, what property is used to indicate the value of the field when the cursor is nowhere near it? When I click on the DataGrid cell to display the combobox, the proper value is populated. But I should see that value when the cell isn't selected as well. Perhaps I'm naive but I think this column should display its value somehow when not active as a focused combobox.

Upvotes: 0

Views: 786

Answers (1)

paparazzo
paparazzo

Reputation: 45106

Because only the EditingElementStyle has an ItemsSource. Why do you have a separate EditingElementStyle when it appears you want the basic editing capabilities of a ComboBox?

Upvotes: 2

Related Questions