如何绑定一个DataTable到一个WPF编辑ComboBox:selectedItem属性显示System.Data.DataRowView

如何绑定一个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"