One approach that should work in both WPF and UWP is to use an Attached Property .
Type a class to manage and group the ListView / Selector :
public class GroupManager
{
public static readonly DependencyProperty IsGroupedProperty = DependencyProperty.RegisterAttached(
"IsGrouped",
typeof(bool),
typeof(GroupManager),
new FrameworkPropertyMetadata(false, OnIsGroupedChanged));
public static bool GetIsGrouped(DependencyObject d)
{
return (bool)d.GetValue(IsGroupedProperty);
}
public static void SetIsGrouped(DependencyObject d, bool value)
{
d.SetValue(IsGroupedProperty, value);
}
private static void OnIsGroupedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var selector = d as Selector;
if (selector == null)
{
throw new
InvalidOperationException("Esta propriedade apenas pode ser aplicada a objectos baseados em Selector");
}
var isGrouped = (bool)e.NewValue;
if (isGrouped)
{
Register(selector);
if (selector.SelectedIndex != -1)
{
UpdateGroupSelection(selector);
}
}
else
{
Unregister(selector);
}
}
private static void Unregister(Selector selector)
{
GroupElements.Remove(selector);
selector.SelectionChanged -= OnSelectionChanged;
}
private static void Register(Selector selector)
{
selector.SelectionChanged += OnSelectionChanged;
GroupElements.Add(selector);
}
private static void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
UpdateGroupSelection(sender as Selector);
}
private static void UpdateGroupSelection(Selector selector)
{
foreach (Selector element in GroupElements)
{
if (element != selector)
{
element.SelectionChanged -= OnSelectionChanged;
element.SelectedIndex = -1;
element.SelectionChanged += OnSelectionChanged;
}
}
}
private static readonly ArrayList GroupElements = new ArrayList(2);
}
This class declares DependencyPropreterty IsGrouped
and is registered as Attached which, when assigned to a ListView , will indicate whether it should be considered in a group in that only one element can have selected items.
Example usage:
....
....
<Grid Margin="0,50,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<ListView x:Name="TopListView" local:GroupManager.IsGrouped="True"/>
<ListView x:Name="BottomListView" Grid.Column="1" local:GroupManager.IsGrouped="True"/>
<ListView x:Name="AnotherListView" Grid.Column="2"
local:GroupManager.IsGrouped="{Binding ElementName=CheckBox, Path=IsChecked}"/>
<CheckBox x:Name="CheckBox" Grid.Column="3"/>
</Grid>
.....
.....
In this example, AnotherListView
only participates in the group when CheckBox
is selected.