2
Answers

WPF.Problem with Combobox.SelectedValue

Eugen

Eugen

14y
9.3k
1
I have a problem with  Combobox.SelectedValue
when I use  this property I don't have the result what this property must return

for eg.:
I have in WPF:

 <ComboBox Grid.Row="5" Grid.Column="2" Height="20" Visibility="Visible" Margin="3" Name="cb_client_cab" ItemsSource="{Binding}" >
                                    <ComboBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding nr_cabinet}" Width="50" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </ComboBox.ItemTemplate>
                                </ComboBox>

I have in C#:
cb_client_cab.SelectedValue.ToString()
The result:
"System.Data.DataRowView"

but not : a value from database , combobox was populated with success, I can choose anything !!!

Help me please!


Answers (2)
1
John Wiese

John Wiese

NA 458 0 14y

This is a problem that is caused by the way binding happens.  In order to get to the value you ar looking for you will need to dig a little, but if you start with this line of code you should be able to get the actual value you want.
((DataRowView)cb_client_cab.SelectedItem).Row.ItemArray
Using this you can find the actual Column you want, which would be the one that you are binding to the Textblock Text property. Then you can reference like this:
((DataRowView)cb_client_cab.SelectedItem).Row[3]
 
0
nip weer

nip weer

NA 3 0 14y

Hi John,
I had an issue with listview before I saw your reply. But after read your answer I solved it , Thank you verymuch.
Cheers.