如何在代码隐藏中选择多选项元素

本文关键字:选择 选项 元素 隐藏 代码 | 更新日期: 2023-09-27 18:37:19

当导航到多选列表页面时,我正在尝试预先选择多选列表中的某些项目,因为一旦从页面导航,我选择的项目就不会保持选中状态。我已经创建了一个列表,其中包含要使用的选定值(名为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>

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

如何在代码隐藏中选择多选项元素

如果您有多重选择列表绑定到的项目列表,则只需将"所选项目"添加到列表的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);
}