开发者

WPF ComboBox get displayed text

I'm having a problem with my combo box and trying to get the text that is displayed in the box. I have the ItemsSource bound to a SqlDataReader, and I'm trying to populate another combo box based on what item is selected in the first combo box.

In the first combobox's selection changed event, I'm running a query based on what's selected in that box. However, I can't get that text for the life of me. Everything I've tried always returns "System.Data.Common.DataRecordInternal". Even when I try "comboBox.Text", it returns that also despite it not开发者_如何学编程 being displayed. Did I do the binding wrong? Or is something wrong with my item template for the combo box?

Here's the relevant code:

<ComboBox Name="cbTables"  ItemsSource="{Binding ElementName=lstTables, Path=ItemsSource}" 
                              SelectionChanged="cbTables_SelectionChanged" >
                        <ComboBox.ItemTemplate>

                            <DataTemplate>
                                <Label Content="{Binding Path=TABLE_NAME}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

Tell me if I need to post anything else. Thanks


I don't see anything wrong with your xaml code. DataRecordInternal is correct item type for sql collection. If you want clr object - bind control to list of clr objects. But if you want just string - you can try this solution:

<ComboBox Name="cbTables" ItemsSource="{Binding ElementName=lstTables, Path=ItemsSource}" 
DisplayMemberPath="TABLE_NAME" SelectedValuePath="TABLE_NAME" SelectionChanged="cbTables_SelectionChanged" />


private void cbTables_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var str = cbTables.SelectedValue;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜