0

当导航到多选列表页面时,我试图预选多选列表中的某些项目,因为一旦导航页面,我选择的项目就不会保持选中状态。我创建了一个包含要使用的选定值的列表(名为 StrobeBrushList 位于名为 Settings.cs 的自定义类中,它使用隔离存储来保存值,并且工作正常),但我不确定如何正确地重新选择当页面从导航然后返回到时的那些项目。

*注意,ColorItem 和 ColorHelper 也是用于获取颜色和值的自定义类

多选列表.xaml

<toolkit:MultiselectList x:Name="ColorList" HorizontalAlignment="Left" VerticalAlignment="Top" Tap="ColorList_Tap">
                <toolkit:MultiselectList.ItemTemplate>
                    <DataTemplate>

                        <StackPanel Orientation="Horizontal" Margin="12,0,0,0" Grid.ColumnSpan="2">
                            <!--<Rectangle Fill="{Binding Brush}" Width="50" Height="50"/>-->
                            <CheckBox Background="{Binding Brush}"/>

                            <TextBlock Text="{Binding Name}" Margin="12,10,0,0"/>
                        </StackPanel>
                    </DataTemplate>
                </toolkit:MultiselectList.ItemTemplate>
            </toolkit:MultiselectList>

多选列表.xaml.cs

List<ColorItem> solidColorBrushList;

public MultiselectlistPage()
    {
        InitializeComponent();

        solidColorBrushList = new List<ColorItem>()
        {
            new ColorItem { Brush = ColorHelper.ToSolidColorBrush("#FFF0F8FF"), Name = "alice blue" },
            new ColorItem { Brush = ColorHelper.ToSolidColorBrush("#FFFAEBD7"), Name = "antique white" },
            new ColorItem { Brush = ColorHelper.ToSolidColorBrush("#FF00FFFF"), Name = "aqua" },
            new ColorItem { Brush = ColorHelper.ToSolidColorBrush("#FF7FFFD4"), Name = "aquamarine" },
            new ColorItem { Brush = ColorHelper.ToSolidColorBrush("#FFF0FFFF"), Name = "azure" },  //dont translate!?
            new ColorItem { Brush = ColorHelper.ToSolidColorBrush("#FFF5F5DC"), Name = "beige" },

                   ...

        };

        this.ColorList.ItemsSource = solidColorBrushList;

        this.Loaded += new RoutedEventHandler(ColorListPage_Loaded);
    }

    void ColorListPage_Loaded(object sender, RoutedEventArgs e)
    {
        //show checkboxes when page is loaded
        this.ColorList.IsSelectionEnabled = true;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (solidColorBrushList == null)
        {
            return;
        }

        ItemContainerGenerator itemContainerGenerator = this.ColorList.ItemContainerGenerator;

        //Settings.StrobeBrushList.Value contains the list of brush items selected by the user
        foreach (SolidColorBrush scB in Settings.StrobeBrushList.Value)
        {
            //this.SetCheckBoxesSelected(true, null);
            if (scB != null)
            {                    
                foreach(ColorItem cI in solidColorBrushList)
                { 
                    //compare the color values of the lists and only select (and show checkmark?) of items in the saved list
                    if (cI.Brush.Color == scB.Color)
                    {
                        DependencyObject vI = itemContainerGenerator.ContainerFromItem(cI);
                        MultiselectItem msI = vI as MultiselectItem;
                        if (msI != null)
                        {
                            msI.IsSelected = true;
                        }
                    }
                }                    
            }
        }
    }
4

1 回答 1

0

如果您有一个多选列表绑定到的项目列表,您可以简单地将“选定项目”添加到列表的SelectedItems集合中,然后再将其添加到绑定列表中。

像这样的一些xaml

<toolkit:MultiselectList ItemsSource="{Binding AllColors}">

并在您的填充代码中

MultiselectList list = /* The list from your xaml */;
foreach (ColorModel model in allModels) {
    ColorViewModel viewModel = new ColorViewModel(model);
    if (shouldBeSelected(model)) {
        list.SelectedItems.Add(viewModel);
    }
    AllColors.Add(viewModel);
}
于 2012-08-30T14:07:34.107 回答