Click here to Skip to main content
15,881,729 members
Articles / Desktop Programming / WPF

Single Selection Across Multiple ItemsControls (v2)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Oct 2012Apache4 min read 6.9K   97   3  
Another approach to implementing single selection across multiple ItemsControls
 <!--***************************************************************************************

	Copyright 2012 Greg Dennis

	   Licensed under the Apache License, Version 2.0 (the "License");
	   you may not use this file except in compliance with the License.
	   You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

	   Unless required by applicable law or agreed to in writing, software
	   distributed under the License is distributed on an "AS IS" BASIS,
	   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	   See the License for the specific language governing permissions and
	   limitations under the License.
 
	File Name:		MainWindow.xaml
	Namespace:		SingleSelection
	Class Name:		MainWindow
	Purpose:		Provides layout of the MainWindow class.

***************************************************************************************-->
<Window x:Class="SingleSelection.MainWindow"
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:local="clr-namespace:SingleSelection"
		Title="MainWindow" Height="350" Width="525">
	<Window.Resources>
		<DataTemplate x:Key="IntsAndDatesTemplate" DataType="{x:Type local:ISelectable}">
			<Border x:Name="Border" Background="Transparent"
					local:SelectionManager.ManageSelection="True"
					local:SelectionManager.Scope="IntsAndDates"
					local:SelectionManager.Target="{Binding}">
				<TextBlock x:Name="Content" Text="{Binding Value}"/>
			</Border>
			<DataTemplate.Triggers>
				<DataTrigger Binding="{Binding IsSelected}" Value="True">
					<Setter TargetName="Border" Property="Background"
							Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"/>
					<Setter TargetName="Content" Property="Foreground"
							Value="{StaticResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
				</DataTrigger>
			</DataTemplate.Triggers>
		</DataTemplate>
		<DataTemplate x:Key="StringsTemplate" DataType="{x:Type local:ISelectable}">
			<Border x:Name="Border" Background="Transparent"
					local:SelectionManager.ManageSelection="True"
					local:SelectionManager.Scope="Strings"
					local:SelectionManager.Target="{Binding}">
				<TextBlock x:Name="Content" Text="{Binding Value}"/>
			</Border>
			<DataTemplate.Triggers>
				<DataTrigger Binding="{Binding IsSelected}" Value="True">
					<Setter TargetName="Border" Property="Background"
							Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}"/>
					<Setter TargetName="Content" Property="Foreground"
							Value="{StaticResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
				</DataTrigger>
			</DataTemplate.Triggers>
		</DataTemplate>
	</Window.Resources>
	<Grid>
		<Grid.ColumnDefinitions>
			<ColumnDefinition />
			<ColumnDefinition />
			<ColumnDefinition />
		</Grid.ColumnDefinitions>
		<ListBox ItemsSource="{Binding Numbers}" HorizontalContentAlignment="Stretch"
				 ItemTemplate="{StaticResource IntsAndDatesTemplate}" />
		<ListBox Grid.Column="1" ItemsSource="{Binding Dates}"
				 ItemTemplate="{StaticResource IntsAndDatesTemplate}"
				 HorizontalContentAlignment="Stretch" />
		<ListBox Grid.Column="3" ItemsSource="{Binding Strings}"
				 ItemTemplate="{StaticResource StringsTemplate}"
				 HorizontalContentAlignment="Stretch" />
	</Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions