One approach that should work on both WPF and UWP is to use a Attached Property.
Write a class to manage and group 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);
}
Dependencypropreterty is declared in this class IsGrouped
and registered as Attached which, when assigned to a Listview(Selector), indicate whether it should be considered in a group where only one of the elements may have selected items.
Example of use:
....
....
<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 the AnotherListView
participates in the group only when the CheckBox
is selected.
The worst is that not even using the event
SelectionChanged
is being as simple as I thought it would be. The problem is that defining the propertySelectedIndex
via code also triggers the eventSelectionChanged
. I tried to get around using the propertyFocusState
so that he can only make the change if theListView
does not have the focus, but has no effect. code of Event handlers.– Matheus Saraiva
See help: http://stackoverflow.com/questions/359424/mutually-exclusive-selection-of-two-listviews
– rubStackOverflow
In WPF it is easy to do using a Targetedtriggeraction and Interaction.Triggers. I do not know if it is possible to use them in UWP or if there is something equivalent. If you find it useful, tell me I’ll put an answer.
– ramaral
@ramaral It will be good, it can help because
UWP
andWPF
share many functionality, think until most.– Matheus Saraiva
So, for the answer to be valid, I will add the tag
WPF
to the question.– ramaral