简体   繁体   中英

Why does the exact same code produce different results?

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 (eg, "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)?

Are the values in your comboBox integers or strings? According to the Remarks on the MSDN Documentation , if the value you set for SelectedValue is not in the comboBox, then it clears the selection (ie sets it to null). If they're integers, your ToString() might not be finding the matching item.

Since you're having a different problem, I don't know if it's best for me to add a new answer, or to edit my earlier one. Since you're asking a different question, I thought it made more sense to add a new answer, but I'll let the mods sort it out.

From what you've described, it sounds like your comboBox could really just contain the integers 0 - 59, but you want them all to appear as 2 digits (anything less than 10 should have a leading zero).

If this is the case, take a look at the ItemStringFormat property on the ComboBox. Forgive me for stating the obvious, but this lets you specify a format string for displaying the items in your comboBox. You can use the standard .NET format string to specify your leading zero, and just use integers to populate the comboBox. Hopefully this will take care of your "type morphing" issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM