Click here to Skip to main content
15,893,622 members
Articles / Desktop Programming / WPF

A Simple WPF ComobBox based Brush Selector Control

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
18 Jul 2011CPOL9 min read 27.1K   865   8  
This shows a couple of techniques using WPF to create a control to select a SolidColorBrush via a ComboBox and compares and contrasts them.
<Window x:Class="BrushSelector.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:src="clr-namespace:BrushSelector"
        Title="MainWindow" Height="350" Width="525">
	<Window.Resources>
		<Style TargetType="ComboBox" x:Key="BrushSelector">
			<Setter Property="ItemsPanel">
				<Setter.Value>
					<ItemsPanelTemplate>
						<UniformGrid/>
					</ItemsPanelTemplate>
				</Setter.Value>
			</Setter>

			<Setter Property="ItemTemplate">
				<Setter.Value>
					<DataTemplate DataType="{x:Type SolidColorBrush}">
						<Rectangle Width="18" Height="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Width}" Margin="2" Fill="{Binding}"/>
					</DataTemplate>
				</Setter.Value>
			</Setter>
		</Style>
		
		<Style TargetType="ComboBox" x:Key="BrushesSelector" BasedOn="{StaticResource BrushSelector}">
			<Setter Property="ItemsSource" Value="{x:Static src:BrushesToList.Brushes}"/>			
		</Style>	
		
		<x:Array x:Key="SomeBrushes" Type="{x:Type SolidColorBrush}">
			<SolidColorBrush>Red</SolidColorBrush>
			<SolidColorBrush>Green</SolidColorBrush>
			<SolidColorBrush>Blue</SolidColorBrush>
		</x:Array>

	</Window.Resources>
	
	<Grid>
		<Grid.RowDefinitions>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="Auto"/>
		</Grid.RowDefinitions>
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="Auto"/>
			<ColumnDefinition Width="Auto"/>
			<ColumnDefinition Width="*"/>
		</Grid.ColumnDefinitions>
		<Label Grid.Column="0" Grid.Row="0" VerticalAlignment="Center">Any Brush Selector</Label>
		<ComboBox Grid.Column="1" Grid.Row="0" Name="RGBBrushSel" VerticalAlignment="Center" Style="{StaticResource BrushSelector}" ItemsSource="{StaticResource SomeBrushes}"  SelectedIndex="0"/>
		<Ellipse Grid.Column="2" Grid.Row="0" Width="120" Height="60" Fill="{Binding ElementName=RGBBrushSel, Path=SelectedItem}"/>

		<Label Grid.Column="0" Grid.Row="1" VerticalAlignment="Center">Brushes Selector</Label>
		<ComboBox Grid.Column="1" Grid.Row="1" Name="BrushSel" VerticalAlignment="Center" Style="{StaticResource BrushesSelector}" SelectedIndex="0"/>
		<Ellipse Grid.Column="2" Grid.Row="1" Margin="5" Width="120" Height="60" VerticalAlignment="Center" Fill="{Binding ElementName=BrushSel, Path=SelectedItem}"/>

		<Label Grid.Column="0" Grid.Row="2" VerticalAlignment="Center">User Control</Label>
		<src:BrushSelUserControl Grid.Column="1" Grid.Row="2" x:Name="UserCtrl" VerticalAlignment="Center" SelectedIndex="77"/>
<Ellipse Grid.Column="2" Grid.Row="2" Margin="5" Width="120" Height="60" VerticalAlignment="Center" Fill="{Binding ElementName=UserCtrl, Path=SelectedItem}"/>		
	</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 Code Project Open License (CPOL)


Written By
Team Leader
United Kingdom United Kingdom
My day job is mostly working in C++ with a bit of C#. I write a fair amount of command line based tools and really wish they could have a GUI front-end to them hence why I spend my spare time working with WPF.

I started a blog few years back but didn't do a lot with it. I've started describing some of the interesting programming things I come across on it. Please take a look.

Comments and Discussions