如何绑定一个DataTable到WPF编辑ComboBox:将selectedItem显示Syste

2019-09-29 16:23发布

我必将DataTable添加到组合框,并确定在itemTemplate.i一个DataTemplate可以在组合框的下拉列表中看到所需的价值观,我在将selectedItem看到的是System.Data.DataRowView这里是我的代码:

 <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>

后面的代码是这样:

 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做同样类型的数据模板,但我没有发现。

我想问一下,如果我做错了什么,或者它的组合框结合一个已知的问题?
如果事情是错在我的代码,请点我朝着正确的方向。
感谢您阅读本

Answer 1:

默认情况下,组合框将调用toString()所选择的项目获得的值,显示 - 因为你是绑定到DataRowView的结果是类型(的ToString的默认行为())

你真正想要什么,以显示其所选项目的用户名和财产要做到这一点,你可以设置的DisplayMemberPath组合框,以“用户名”

(另外,如果你这样做,你可能会发现,你可以摆脱自定义的DataTemplate太大,因为用户名也将被用于填充每个项目。)


在回答您的评论:

我不想被那些程序员之一,但“它的工作原理在我的机器”。

我XMAL是:

 <ComboBox Name="cmbEmail" 
            Height="23" 
            VerticalAlignment="Top" 
            TabIndex="1" 
            ToolTip="enter the email you signed up with here"
            IsEditable="True"
            IsSynchronizedWithCurrentItem="True" 
            ItemsSource="{Binding}"
            DisplayMemberPath="username">
  </ComboBox> 

我后面的代码是:

public partial class Window1 : Window
{
    public Window1()
    {
        Users = new DataTable("users");
        Users.Columns.Add("username");

        Users.Rows.Add(CreateDataRow("Fred"));
        Users.Rows.Add(CreateDataRow("Bob"));
        Users.Rows.Add(CreateDataRow("Jim"));

        InitializeComponent();

        cmbEmail.DataContext = Users;
    }

    public DataTable Users { get; private set; }

    private DataRow CreateDataRow(string userName)
    {
        DataRow dr = Users.NewRow();
        dr["username"] = userName;

        return dr;
    }
}


Answer 2:

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

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

原因是

由于组合框被设置为IsEditable =“真”,和组合框包含一个TextBox元件以显示所选择的值。 但ComboBox中产品RowView类型; 它示出了类型信息不是在文本框的值。

注意

TestSearch.TextPath="username"


Answer 3:

我发现了为什么它不working.Using的DisplayMemberPath不工作,而是ItemTemplate中。 下面是一个例子。

<Window.Resources>
  <DataTemplate x:Key="comboTemplate">
        <TextBlock Text="{Binding Path=username}" />
  </DataTemplate>
</Window.Resources>
<ComboBox Margin="18,121,24,0" Name="cmbEmail" Tag="email" TabIndex="1" ToolTip="enter the email you signed up with here" IsEditable="True" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource comboTemplate}"  ItemsSource="{Binding}" Height="23" VerticalAlignment="Top" Style="{DynamicResource cmbBoxerrors}">
            <ComboBox.Text>
                <Binding Path="username"/>
            </ComboBox.Text>       
 </ComboBox>

注意ItemTemplate中

该xaml.cs代码不会改变。 感谢那些谁读和建议的解决方案给我。



文章来源: How to bind a datatable to a wpf editable combobox: selectedItem showing System.Data.DataRowView