如何在代码后面选择Multiselectitem元素

时间:2012-06-16 03:30:47

标签: c# windows-phone-7 xaml

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

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

Multiselectlist.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>

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;
                        }
                    }
                }                    
            }
        }
    }

1 个答案:

答案 0 :(得分: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);
}