0

我正在尝试使用 MultiSelectList 中当前选定的项目填充 ObservableCollection。选择的方法涉及一个复选框控件,我不确定如何让选定的项目填充 ObservableCollection。我已经引用了两种方法,它们执行 MultiSelectList 的单个选择以及 MultiSelectList 的“全选”选项。

需要注意的是,networkSelectList 是自定义 Settings 类中的 ObservableCollection。

主页.xaml

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

            <StackPanel Orientation="Horizontal"  Margin="12,0,0,0">
                <Image Source="{Binding Icon}" Width="35" Height="35" Margin="0"/>
                <TextBlock Text="{Binding Name}" TextAlignment="Center"  Margin="10"/>
            </StackPanel>

        </DataTemplate>
    </toolkit:MultiselectList.ItemTemplate>                            
</toolkit:MultiselectList>

MainPage.xaml.cs

/// <summary>
    /// method to Select All and UnSelect All checkboxes
    /// </summary>
    /// <param name="selected"></param>
    /// <param name="predicate"></param>
    private void SetCheckBoxesSelected(bool selected, Predicate<ConnectionItem> predicate)
    {
        if (networkTypeList == null)
        {
            return;
        }
        if (predicate == null)
        {
            predicate = (networkInfo) => true;
        }


        ItemContainerGenerator itemContainerGenerator = this.connectionTypeMultiSelectList.ItemContainerGenerator;


        foreach (ConnectionItem networkInfo in networkTypeList)
        {
            if (networkInfo != null && predicate(networkInfo))
            {
                DependencyObject visualItem = itemContainerGenerator.ContainerFromItem(networkInfo);
                MultiselectItem multiselectItem = visualItem as MultiselectItem;
                if (multiselectItem != null)
                {
                    multiselectItem.IsSelected = selected;

                    //add selected item to networkSelectionChecked  ??
                    //Settings.networkSelectionChecked.Value.Add(multiselectItem.Name.ToString());
                }
            }
        }
    }

    /// <summary>
    /// triggered on tap of any item in connectionTypeMultiSelectList
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void connectionTypeMultiSelectList_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        DependencyObject tappedElement = e.OriginalSource as UIElement;
        MultiselectItem tappedItem = this.FindParentOfType<MultiselectItem>(tappedElement);
        ConnectionItem selectedItem = null;
        if (tappedItem != null)
        {
            // DataContext contains reference to data item
            selectedItem = tappedItem.DataContext as ConnectionItem;
        }


        if (selectedItem != null)
        {
            MessageBox.Show(selectedItem.Name + "Tapped");

            //add selected item to networkSelectionChecked  ??
            //Settings.networkSelectionChecked.Value.Add(multiselectItem.Name.ToString());

        }
    }

    /// <summary>
    /// method to find out the element in VisualTree
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="element"></param>
    /// <returns></returns>
    private T FindParentOfType<T>(DependencyObject element) where T : DependencyObject
    {
        T result = null;
        DependencyObject currentElement = element;
        while (currentElement != null)
        {
            result = currentElement as T;
            if (result != null)
            {
                break;
            }
            currentElement = VisualTreeHelper.GetParent(currentElement);
        }
        return result;
    }

    void unselectAll_Click(object sender, EventArgs e)
    {
        this.SetCheckBoxesSelected(false, null);
        this.connectionTypeMultiSelectList.IsSelectionEnabled = false;
    }

    void selectAll_Click(object sender, EventArgs e)
    {
        this.SetCheckBoxesSelected(true, null);
    }

所以基本上我试图将每个被选中的项目添加到 ObservableCollection 中。我想完成此操作,以便能够创建辅助图块并通过传递表示所选复选框的查询字符串来更新它们。我如何才能正确地将选定的复选框项添加到 ObservableCollection?同样在类似的意义上,我将如何检测何时取消选择项目并从 ObservableCollection 中删除该项目?此外,是否有正确的方法将所选项目保留在 MultiSelectList 中,以便可以将其保存以供将来启动或激活的应用程序使用?

4

1 回答 1

1

您可以从 SelectedItems 属性中获取所选项目。

var selectedItems = new ObservableCollection<ConnectionItem>(connectionTypeMultiSelectList.SelectedItems.Count);
foreach (var item in TextItemsList.SelectedItems)
{
    var connectionItem = item as ConnectionItem;
    if (connectionItem == null) continue;
    selectedItems.Add(connectionItem );
}

SelectionChanged 事件将让您知道何时(取消)选择项目。

void MultiSelectListSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    foreach(var item in e.AddedItems)
    {
        // add to collection
    }
    foreach(var item in e.RemovedItems)
    {
        // remove from collection
    }
}
于 2012-08-15T04:59:01.643 回答