After this code:
if ((App.roamingSettings.Values.ContainsKey("FromMonthSection1")) &&
(!string.IsNullOrWhiteSpace(App.roamingSettings.Values["FromMonthSection1"].ToString())))
{
comboBoxFromMonth.SelectedValue = App.roamingSettings.Values["FromMonthSection1"].ToString();
}
if ((App.roamingSettings.Values.ContainsKey("FromDaySection1")) &&
(!string.IsNullOrWhiteSpace(App.roamingSettings.Values["FromDaySection1"].ToString())))
{
comboBoxFromDay.SelectedValue = App.roamingSettings.Values["FromDaySection1"].ToString();
}
...runs, comboBoxFromMonth's SelectedValue = "August," as expected. However, the SelectedValue of comboBoxFromDay = null, even though in the Immediate Window, I see:
App.roamingSettings.Values["FromDaySection1"].ToString()
"16"
comboBoxFromDay.Items.Count
31
Stepping through it, the assignment statement runs in both cases, it's just that comboxFromDay.SelectedValue is null even after the assignment for some reason.
So what could be different that is causing these opposite reactions with the exact same code?
The xaml for the two comboBoxes in question is:
<ComboBox x:Name="comboBoxFromMonth" Height="24" Width="100" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="4" LostFocus="ComboBox_OnLostFocus" ></ComboBox>
<ComboBox x:Name="comboBoxFromDay" Grid.Row="1" Grid.Column="1" Height="24" Width="80" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="2" LostFocus="ComboBox_OnLostFocus"/>
The XAML is for all practical purposes exactly the same, except that comboBoxFromMonth has no explicit Grid.Row or Grid.Column assignment.
Also, setting the roaming values is exactly the same:
if (cmbxName == "comboBoxFromMonth")
{
App.roamingSettings.Values["FromMonthSection1"] =
comboBoxFromMonth.SelectedValue;
}
else if (cmbxName == "comboBoxFromDay")
{
App.roamingSettings.Values["FromDaySection1"] =
comboBoxFromDay.SelectedValue;
}
So how is it that in one case it works, and in the other it doesn't?
BTW, the properties as viewed in the Properties pane for the two comboboxes are the same, too (except Name, of course; and MaxDropDownHeight - 520 vs. Infinity, but I'm sure that's irrelevant).
RESOLVED (OR SO I THOUGHT)
Brandon hit the nail on the head: removing the ".ToString()":
if ((App.roamingSettings.Values.ContainsKey("FromDaySection1")) &&
(!string.IsNullOrWhiteSpace(App.roamingSettings.Values["FromDaySection1"].ToString())))
{
comboBoxFromDay.SelectedValue = App.roamingSettings.Values["FromDaySection1"];
}
...works.
UPDATE
Okay, so it wasn't quite solved after all yet; there's something strange I've noticed about this:
The roaming settings engine apparently decides what the data type of the values you're saving are.
If you save values such as "January" it saves that as a string. If you save values such as "1" through "12" it saves that as an int.
However, what if you have values such as "00" (appropriately viewed as a string) AND values such as "10" through "59"? Although stored in the same bucket (e.g., "ToMinuteSection1"), the data type will morph.
This makes sense, as the bucket only has one value at a time. If it's "00", it's seen as a string; if it's "10" it's seen as an int.
But this makes it a little kludgy when it comes to retrieving those values, because this works if the value contained in "ToMinuteSection1" is a string (such as "00"):
comboBoxToMinute.SelectedValue = App.roamingSettings.Values["ToMinuteSection1"].ToString();
...but not if the value is seen as an int (such as "59"); then it needs to be:
comboBoxToMinute.SelectedValue = App.roamingSettings.Values["ToMinuteSection1"];
SO...since I don't know what the user is going to select, I can do this, but it seems like something I should get slapped for:
int toMinute;
string toMinuteStr = App.roamingSettings.Values["ToMinuteSection1"].ToString();
bool isInt = int.TryParse(toMinuteStr, out toMinute);
if (isInt) // it should alway be an int now
{
if (toMinute < 10)
{
comboBoxToMinute.SelectedValue = toMinuteStr;
}
else
{
comboBoxToMinute.SelectedValue = toMinute;
}
}
...and actually, I tried to do it this way first:
int toMinute;
string toMinuteStr = App.roamingSettings.Values["ToMinuteSection1"].ToString();
bool isInt = int.TryParse(toMinuteStr, out toMinute);
if (isInt)
{
comboBoxToMinute.SelectedValue = toMinute;
}
else
{
comboBoxToMinute.SelectedValue = toMinuteStr;
}
...but that trick doesn't work, because "09" is parsed into 9 just fine, but the assignment is null because there is no "9" in the combobox, just a "09"
Is there a more elegant way to accomplish this (than my functioning my slapworthy method shown above)?