【问题标题】:WPF C# ComboBox Display ValueWPF C# 组合框显示值
【发布时间】:2016-05-29 00:22:48
【问题描述】:

我不能正确地问这个问题。我已经在 3 个不同的论坛上问过它,并在谷歌上搜索了大约一周,但一无所获。所以让我在这里再试一次,看看用不同的方式问它是否有助于你帮助我......

使用 MahApps.Metro 的 WPF C# MetroWindow 窗口 - 组合框 - 州代码(美国 50 个州) 我创建了一个数据集并将数据集拖到 XAML 中的组合框。 这是 XAML

<ResourceDictionary>
    <CollectionViewSource x:Key="StatesViewSource" 
                          Source="{Binding States, Source={StaticResource PRx}}" />
</ResourceDictionary>

在 GRID 中我设置了数据上下文:

<Grid DataContext="{StaticResource StatesViewSource}">

然后是组合框

<ComboBox x:Name="CbState" 
      SelectedValuePath="StateCode" 
      ItemsSource="{Binding}"
      DisplayMemberPath="StateCode" />

现在,如果就是这样,我停下来,它就可以了。我在组合框中得到了州代码;但是,这不是问题或我需要解决的问题。我需要做的是查询数据库,找到病人,并将地址返回到窗口。除组合框外,每个字段都填充在窗口中。它始终默认为列表中的第一个状态 - AL - 值,即使患者的状态代码是 MA。

所以,SQL 有效。组合框拉取值。但我无法让组合框默认为通过 SQL 返回的 MA。

这是我也在使用的一些 C# 代码。

private void ReturnPatient()
{
    var patIdReturn = TxtPatId.Text;
    var patSiteId = TxtTSiteCode.Text;
    var preSql = Settings.Default.GetPatientProfile;
    var param = " @p1 = '" + patIdReturn + "', @p2 = '" + patSiteId + "';";
    var sql = preSql + param;
    var connectionString = Settings.Default.Dbconn;
    using (var conn = new SqlConnection(connectionString))
    using (var cmd = new SqlCommand(sql, conn))
    {
        try
        {
            conn.Open();
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                TxtFirstName.Text = reader[0] as string;
                TxtMi.Text = reader[1] as string;
                TxtLastName.Text = reader[2] as string;
                TxtLegacy.Text = reader[3] as string;
                DobPicker.SelectedDate = reader[4] as DateTime? ?? new DateTime();
                TxtPhone.Text = reader[5] as string;
                TxtAddr1.Text = reader[6] as string;
                TxtAddr2.Text = reader[7] as string;
                TxtCity.Text = reader[8] as string;
                TbState.Text = reader[9] as string;
                // trying a tbox instead of combobox, but this is where I want the Combobox for State. 
                TxtZip.Text = reader[10] as string;
                //break for single row or you can continue if you have multiple rows...
                break;
            }

            Log.Debug("Command executed and reader completed for ReturnPatient()");
        }
        catch (SqlException ex)
        {
            // error handling
        }
        finally
        {
            // logging
        }
    }
}

我尝试过 CbState.SelectedItem、CbState.SelectedValue、CbState.DisplayMemeber 等...

拜托,我知道这是可能的。请帮忙

此致,

迷失在组合框地狱中:)

【问题讨论】:

  • 一些提示:检查reader[9] 的实际结果。在这里使用数字偏移量是一种不好的做法,因为当您的基础查询或表结构发生变化时,您会遇到问题。检查此链接:stackoverflow.com/questions/3391195/…

标签: c# sql wpf combobox selectedvalue


【解决方案1】:
cb_State.SelectedItem = (cb_State.ItemsSource as List<YourStateCodeClass>).Where(s=>s.StateCode == reader[9] as string).First();

但我没有看到您的 StateCode 类,但您使用 DisplayMemberPath 等。

如果它只是你可以使用的字符串

cb_State.SelectedItem = (cb_State.ItemsSource as List<string>).Where(s => s == reader[9] as string).First();

并删除 DisplayMemberPathSelectedValuePath

【讨论】:

    【解决方案2】:

    这似乎很简单,你错过了基本点。如果单击组合框,您能看到 50 个州的名称填充为绑定项吗?如上所述,检查 reader[9] 的值是否真的是 MA。我建议更具体地尝试像 reader["yourcolumnname"] 以避免混淆。

    然后试试 CbState.SelectedText="MA";
    或 CbState.Text="MA";

    selected- 前缀可能意味着它必须由用户有意选择。

    并确保在 XML 中 selectedindex= -1。 如果索引为 = 0 ,则组合框会将第一个填充项 AL 视为默认值。

    最后,如果你只需要将已经固定的 50 个状态名称放入组合框,为什么不简化代码结构而不像

    那样绑定
    CbState.Items.Add("AL");
     ....
    CbState.Items.Add("MA");
    CbState.Items.Add("NY");
    

    对计算机来说,添加已经固定的 50 个名称将比绑定更简单、更快捷。如果您不需要进行更高级别的工作,请使用绑定。

    作为不相关的小问题,阅读器和连接应该以正确的方式关闭。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 2013-03-04
      • 2010-12-07
      • 1970-01-01
      • 2011-12-21
      相关资源
      最近更新 更多