0

I have a SQL table which has columdns "AreaCode" and "Description" whcih is used to populate a WPF combobox. But when I select an item on the combobox it displays System.Data.DataRowView on the combobox text. Below is the xaml for the combobox.

                    <ComboBox x:Name="AreaComboBox" Grid.Row="3" Grid.Column="1" Width="300" Margin="5" IsEditable="True" ItemsSource="{Binding}" SelectedValuePath="Description">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock x:Name="Area" Text="{Binding AreaCode}"/>
                                    <TextBlock Text=" "/>
                                    <TextBlock x:Name="Description" Text="{Binding Description}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

The combo box is filled using a simple c# code: AreaComboBox.ItemsSource = dataSet.Area.AsDataView();

What I noticed was that this is not a problem if I change IsEditable to False, this is only a problem when its true.

Can someone please help me figure out why its returning "System.Data.DataRowView" rather that the text of the item I selected? Would be even better if there is a solution :)

I am more than happy to provide more details.

  • This answer may help you with finding a solution: https://stackoverflow.com/a/3672098/9365244 – JayV Nov 21 '20 at 06:54
  • You are binding the itemsource twice? In code-behind and also in xaml? – Bandook Nov 21 '20 at 10:25
  • @Bandook unfortunately this was after many attempts to get it working which includes trying different ways of binding. this all had the same result :(. Or do you think this could be the problem? – Shiblee Marsuik Nov 21 '20 at 21:30
  • i havent tested this but im going to take a guess. by doing "ItemsSource={Binding}" you are binding the itemssource to the datacontext of the view itself, and in your code-behind you are binding to the return collection of "dataSet.Area.AsDataView()". When IsEditable is changed, which binding takes precedence changes. This could be wrong, but I'd first like to eliminate double binding scenario - i'd test your code by first removing the binding in the xaml. if that doesn't work, put it back and remove the code-behind binding. BUT more importantly, this is not a good binding to ItemsSource. – Bandook Nov 22 '20 at 05:24
  • My request - please provide us with the following information - (1) When/Where do you call the "dataSet.Area.AsDataView()" i.e. show the method where you set the itemssource in code-behind and (2) what is the return type of this "dataSet.Area.AsDataView()". – Bandook Nov 22 '20 at 05:25

1 Answers1

0

You can set an event MouseLeftButtonDown="StackPanel_MouseDown" for the StackPanel, then use below code to update the Text for the ComboBox.

 private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
    {
        string tb1 = ((TextBlock)(sender as StackPanel).Children[0]).Text;
        string tb2 = ((TextBlock)(sender as StackPanel).Children[1]).Text;
        string tb3 = ((TextBlock)(sender as StackPanel).Children[2]).Text;

        AreaComboBox.Text = tb1 +"   "+ tb3;

    }
DasiyTian_1203
  • 1,038
  • 1
  • 6
  • 16