如何绑定一个DataTable到一个WPF编辑ComboBox:selectedItem属性显示System.Data.DataRowView [英] How to bind a datatable to a wpf editable combobox: selectedItem showing System.Data.DataRowView

查看:1118
本文介绍了如何绑定一个DataTable到一个WPF编辑ComboBox:selectedItem属性显示System.Data.DataRowView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我约束的数据表到一个ComboBox,并确定在itemTemplate.i一个DataTemplate可以看到组合框下拉列表中所需的值,我在则selectedItem看到的是 System.Data.DataRowView 这里是我的codeS:


I bound a datatable to a combobox and defined a dataTemplate in the itemTemplate.i can see desired values in the combobox dropdown list,what i see in the selectedItem is System.Data.DataRowView here are my codes:

 <ComboBox Margin="128,139,123,0" Name="cmbEmail" Height="23" VerticalAlignment="Top" TabIndex="1" ToolTip="enter the email you signed up with here" IsEditable="True" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}">

            <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                      <TextBlock Text="{Binding Path=username}"/>
                </StackPanel>

            </DataTemplate>
        </ComboBox.ItemTemplate>

在code的背后,是这样:

The code behind is so :

 if (con != null)
 {
    con.Open();
    //users table has columns id | username | pass
    SQLiteCommand cmd = new SQLiteCommand("select * from users", con);
    SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
    userdt = new DataTable("users");
    da.Fill(userdt);
    cmbEmail.DataContext = userdt;

 }

我一直在寻找类似SelectedValueTemplate或SelectedItemTemplate做同样类型的数据模板,但我没有发现。

I've been looking for something like SelectedValueTemplate or SelectedItemTemplate to do the same kind of data templating but i found none.

我就想问一下,如果我做错了什么,或者它是一个已知的问题为组合框绑定?结果
如果事情是错误的,我code请点我到正确的方向。结果
 感谢您阅读本

I'll like to ask if i'm doing something wrong or it's a known issue for combobox binding?
if something is wrong in my code please point me to the right direction.
thanks for reading this

推荐答案

我得到的回答形式MSDN和它为我做

i got an answer form msdn and it did it for me

<ComboBox IsEditable="True" ItemTemplate="{StaticResource comboTemplate}" ItemsSource="{Binding}" TextSearch.TextPath="username">

原因是

由于该组合框设置为IsEditable =真,而组合框包含一个TextBox元素以显示所选值。但在组合框中产品RowView类型;它显示的类型信息不是在文本框中的值。

Since the ComboBox is set to IsEditable="True", and the ComboBox contains a TextBox element to show the selected value. But the item in the ComboBox is RowView type; it shows the type information not the value in the TextBox.

注意

TestSearch.TextPath="username"

这篇关于如何绑定一个DataTable到一个WPF编辑ComboBox:selectedItem属性显示System.Data.DataRowView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆