双向绑定设置问题

Jam*_*mie 8 c# silverlight xaml windows-phone-7

我在使用listpicker进行双向绑定时遇到问题.我能够使用c#设置值,但不能SelectedItem=".."在xaml中设置.绑定返回正确的值(并且是listpicker中的值),因为我通过将文本分配给文本块来发短信.

页面加载时,listpicker上使用的绑定会导致a System.ArgumentOutOfRangeException

我用来设置它的代码是:

    // Update a setting value. If the setting does not exist, add the setting.
    public bool AddOrUpdateValue(string key, Object value)
    {
        bool valueChanged = false;

        try
        {
            // If new value is different, set the new value
            if (settingsStorage[key] != value)
            {
                settingsStorage[key] = value;
                valueChanged = true;
            }
        }
        catch (KeyNotFoundException)
        {
            settingsStorage.Add(key, value);
            valueChanged = true;
        }
        catch (ArgumentException)
        {
            settingsStorage.Add(key, value);
            valueChanged = true;
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception occured whilst using IsolatedStorageSettings: " + e.ToString());
        }

        return valueChanged;
    }


    // Get the current value of the setting, if not found, set the setting to default value.
    public valueType GetValueOrDefault<valueType>(string key, valueType defaultValue)
    {
        valueType value;

        try
        {
            value = (valueType)settingsStorage[key];
        }
        catch (KeyNotFoundException)
        {
            value = defaultValue;
        }
        catch (ArgumentException)
        {
            value = defaultValue;
        }

        return value;
    }

    public string WeekBeginsSetting
    {
        get
        {
            return GetValueOrDefault<string>(WeekBeginsSettingKeyName, WeekBeginsSettingDefault);
        }
        set
        {
            AddOrUpdateValue(WeekBeginsSettingKeyName, value);
            Save();
        }
    }
Run Code Online (Sandbox Code Playgroud)

在xaml中:

<toolkit:ListPicker x:Name="WeekStartDay" 
                    Header="Week begins on" 
                    SelectedItem="{Binding Source={StaticResource AppSettings},
                                           Path=WeekBeginsSetting, 
                                           Mode=TwoWay}">
    <sys:String>monday</sys:String>
    <sys:String>sunday</sys:String>
</toolkit:ListPicker>
Run Code Online (Sandbox Code Playgroud)

StaticResource AppSettings是一个单独的.cs文件中的资源.

<phone:PhoneApplicationPage.Resources>
    <local:ApplicationSettings x:Key="AppSettings"></local:ApplicationSettings>
</phone:PhoneApplicationPage.Resources>
Run Code Online (Sandbox Code Playgroud)

提前致谢

Fre*_*lad 5

我使用Reflector来查找此异常的来源.在ListPicker.cs中,将覆盖以下方法.

protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
Run Code Online (Sandbox Code Playgroud)

在此方法中,如果设置了SelectedItem并且SelectedIndex为-1(除非在加载之前设置它,否则以下行)将导致异常.如果未设置SelectedItem,则永远不会到达此行(因此也不例外).

else if (!object.Equals(base.get_Items().get_Item(this.SelectedIndex), this.SelectedItem))
Run Code Online (Sandbox Code Playgroud)

要解决这个问题(直到他们得到修复),我有一些建议.

解决方法1

如果您知道将由TwoWay绑定生成的起始索引,那么您也可以设置SelectedIndex属性并且TwoWay绑定将起作用

<toolkit:ListPicker x:Name="WeekStartDay"
                    Header="Week begins on"
                    SelectedItem="{Binding Source={StaticResource AppSettings},
                                           Path=WeekBeginsSetting,
                                           Mode=TwoWay}"
                    SelectedIndex="1">
    <sys:String>monday</sys:String>
    <sys:String>sunday</sys:String>
</toolkit:ListPicker> 
Run Code Online (Sandbox Code Playgroud)

解决方法2

使用Loaded事件并从那里设置Binding

<toolkit:ListPicker x:Name="WeekStartDay"
                    Header="Week begins on"
                    Loaded="WeekStartDay_Loaded">
    <sys:String>monday</sys:String>
    <sys:String>sunday</sys:String>
</toolkit:ListPicker>

private void WeekStartDay_Loaded(object sender, RoutedEventArgs e)
{
    Binding binding = new Binding();
    binding.Source = this.Resources["AppSettings"] as ApplicationSettings;
    binding.Path = new PropertyPath("WeekBeginsSetting");
    binding.Mode = BindingMode.TwoWay;
    WeekStartDay.SetBinding(ListPicker.SelectedItemProperty, binding);
}
Run Code Online (Sandbox Code Playgroud)