【发布时间】: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