我正在尝试使用MultiSelectList中当前选定的项填充ObservableCollection。选择方法涉及一个复选框控件,我不确定如何让所选项填充ObservableCollection。我引用了两种方法,它们执行MultiSelectList的单个选择以及MultiSelectList的“全选”选项。
要注意,networkSelectList是自定义Settings类中的ObservableCollection。
MainPage.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中,以便可以保存以供将来启动或激活应用程序使用?
答案 0 :(得分: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
}
}