I can't be asking this question right. I've asked it in 3 different forums and searched google for about a week now, and nothing. So let me try it again here and see if asking it a different way helps you help me...
WPF C# MetroWindow using MahApps.Metro Window - Combobox - State Codes (50 US States) I created a dataset and dragged teh data set to the combo box in XAML. Here is the XAML
<ResourceDictionary>
<CollectionViewSource x:Key="StatesViewSource"
Source="{Binding States, Source={StaticResource PRx}}" />
</ResourceDictionary>
Inside the GRID I set the Data Context:
<Grid DataContext="{StaticResource StatesViewSource}">
Then the ComboBox
<ComboBox x:Name="CbState"
SelectedValuePath="StateCode"
ItemsSource="{Binding}"
DisplayMemberPath="StateCode" />
Now, if that's it and I stop, it works. I get the State Codes in the combobox; however, that's the not issue or the problem I need solved. What I need to do is query the DB, find the patient, and return the address to the window. Every field populates in the window except the Combobox. It always defaults to the first state in the list - AL - value, even though the state code for the patient is MA.
So, SQL works. Combobox pulls values. But I can't get the Combobox to default to MA as returned via the SQL.
Here is some C# Code that I'm using as well.
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
}
}
}
I have tried CbState.SelectedItem, CbState.SelectedValue, CbState.DisplayMemeber, etc...
Please, I know this has to be possible. PLEASE HELP
Sincerely,
Lost In ComboBox Hell :)