修改SelectedIndex时,数据绑定ListPicker SelectedItem变为空白

时间:2011-10-05 12:21:35

标签: silverlight data-binding windows-phone-7 silverlight-toolkit listpicker

我的Windows Phone 7应用中有一个ListPicker,其中 ItemsSource SelectedIndex 属性都绑定到我的ViewModel。 SelectedIndex使用双向绑定。应用程序启动时正确填充项目和SelectedIndex。但是,当我在ViewModel中修改SelectedIndex属性时,ListPicker的TextBox变为空白,就好像没有选定的项目一样。如果我进入完整模式并检查列表中的所选项目,则选择正确的项目。

这是ListPicker xaml代码:

<toolkit:ListPicker Name="TheListPicker" ItemsSource="{Binding TheItems}" CacheMode="BitmapCache" FullModeHeader="{Binding Path=Resources.TheHeader, Source={StaticResource LocalizedStrings }}" SelectedIndex="{Binding TheCurrentIndex, Mode=TwoWay}" IsEnabled="{Binding IsViewEnabled}" TabIndex="0" >
    <toolkit:ListPicker.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <TextBlock Text="{Binding Name}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />
            </StackPanel>
        </DataTemplate>
    </toolkit:ListPicker.ItemTemplate>
    <toolkit:ListPicker.FullModeItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="item" Orientation="Horizontal" Margin="5, 24, 0, 24">
                <TextBlock Margin="15, 0, 0, 0" Text="{Binding Name}" FontSize="40" TextWrapping="Wrap" />
            </StackPanel>
        </DataTemplate>
    </toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>

这是我的ViewModel的简化版本:

[DataMember]
public ObservableCollection<ItemEntity> TheItems
{
    get
    {
        if (this.theItems == null)
        {
            this.theItems = new ObservableCollection<ItemEntity>();
        }

        return this.theItems;
    }
    set
    {
        this.theItems = value;
    }
}

[DataMember]
public int TheCurrentIndex
{
    get 
    {
        return this.theCurrentIndex;
    }
    set
    {
        if (value != this.theCurrentIndex)
        {
            this.theCurrentIndex = value;
            NotifyPropertyChanged("TheCurrentIndex");
            NotifyPropertyChanged("IsSomeOtherPropertyEnabled");
        }
    }
}

以下是MainPage.xaml.cs中的相关代码(App_ViewModelChanged是在应用程序启动时执行的某些异步内容完成时调用的事件处理程序):

private void App_ViewModelChanged(object sender, ViewModelChangedEventArgs e)
{    
    BindToViewModel();
}

private void BindToViewModel()
{
    this.DataContext = this.ViewModel;
    this.ViewModel.IsViewEnabled = true;
}

private void SomeAsyncMethodCompleted(object sender, DetectCompletedEventArgs e)
{
    if (e.Error == null)
    {
        this.ViewModel.TheCurrentIndex = e.Result;
    }
}

这个问题并没有一直发生。发生在50%的时间。它似乎只在应用程序生命周期中发生一次,然后再也不会发生。此外,当我从2011年2月发布的Silverlight Control Toolkit发布到2011年8月发布时,问题就开始出现了。从来没有遇到过这个问题。

这是一个已知问题吗?

1 个答案:

答案 0 :(得分:0)

好的,所以这种奇怪行为的原因是2011年8月Silverlight Control Toolkit中ListPickerItem样式的一个小变化。

这就是2011年8月发布的内容:

<Style TargetType="controls:ListPickerItem">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="Padding" Value="8 10"/>
    <Setter Property="Template">
        <!-- More Stuff Here -->
    </Setter>
</Style>

这就是它在2011年2月发布的版本:

<Style TargetType="controls:ListPickerItem">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="Padding" Value="8 6"/>
    <Setter Property="Template">
        <!-- More Stuff Here -->
    </Setter>
</Style>

看到区别?他们在新版本中将ListPickerItem填充更改为 8 10 ,并以某种方式导致问题。恢复 8 6 修复它。不要问我为什么。

所以在我的项目中,我实际上没有修改控件工具包Generic.xaml文件,只是修改了特定的ListPicker资源,为ListPickerItem样式指定了一个新的填充,如下所示:

<toolkit:ListPicker Name="TheListPicker" ItemsSource="{Binding TheItems}" CacheMode="BitmapCache" FullModeHeader="{Binding Path=Resources.TheHeader, Source={StaticResource LocalizedStrings }}" SelectedIndex="{Binding TheCurrentIndex, Mode=TwoWay}" IsEnabled="{Binding IsViewEnabled}" TabIndex="0" >           
    <toolkit:ListPicker.Resources>
        <Style TargetType="toolkit:ListPickerItem">
            <Setter Property="Padding" Value="8 6"/>
        </Style>
    </toolkit:ListPicker.Resources>
    <toolkit:ListPicker.ItemTemplate>           
        <DataTemplate>           
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">           
                <TextBlock Text="{Binding Name}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />           
            </StackPanel>           
        </DataTemplate>           
    </toolkit:ListPicker.ItemTemplate>           
    <toolkit:ListPicker.FullModeItemTemplate>           
        <DataTemplate>           
            <StackPanel x:Name="item" Orientation="Horizontal" Margin="5, 24, 0, 24">           
                <TextBlock Margin="15, 0, 0, 0" Text="{Binding Name}" FontSize="40" TextWrapping="Wrap" />           
            </StackPanel>           
        </DataTemplate>           
    </toolkit:ListPicker.FullModeItemTemplate>           
</toolkit:ListPicker>

这么小的改变!

相关问题