Search...

Saturday, April 26, 2014

Awesome RadioButton Styles and Templates

Introduction

This article describes the styles and templates for the RadioButton control. You can modify the default ControlTemplate to give the control a unique appearance. 
This article is a simple approach for achieving reusable AutoComplete user control.

Skeleton of the Resource

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type RadioButton}" x:Key="buttonStyle1">
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontWeight" Value="DemiBold" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <BulletDecorator Cursor="Hand">

                        <Border CornerRadius="3" BorderBrush="#FFE2E2E2" BorderThickness="1" Padding="5,0,10,0">

                            <StackPanel Orientation="Vertical" Margin="5,0,0,0" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="Center">

                                <ContentPresenter x:Name="cp" Content="{TemplateBinding Content}"/>

                                <Border Name="RadioMark" BorderThickness="1" Width="{Binding ElementName=cp,Path=Width}" Margin="0" Padding="0" HorizontalAlignment="Stretch" Visibility="Collapsed"/>
                               
                            </StackPanel>

                            <Border.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="White" Offset="0" />
                                    <GradientStop Color="#FFE2E2E2" Offset="1" />
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>

                    </BulletDecorator>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="RadioMark" Property="BorderBrush" Value="#FF0092B2"/>
                            <Setter TargetName="RadioMark" Property="Visibility" Value="Visible"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter TargetName="RadioMark" Property="Visibility" Value="Collapsed"/>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type RadioButton}" x:Key="buttonStyle2">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <BulletDecorator Background="White" Cursor="Hand">
                        <BulletDecorator.Bullet>

                            <Border x:Name="bdrButtonStyle2" CornerRadius="3,0,0,3" BorderBrush="#FF565656" BorderThickness="1" Height="{TemplateBinding Height}" Width="{TemplateBinding Height}">
                                <Polygon Name="TickMark"
                                         Points="4,9 8,15 15,3 8,11 4,9" 
                                         Stroke="#FF0092B2"
                                         StrokeThickness="1"
                                         Visibility="Hidden"
                                         Fill="Beige"
                                         HorizontalAlignment="Center"
                                         VerticalAlignment="Center">
                                </Polygon>
                            </Border>

                        </BulletDecorator.Bullet>

                        <Border Background="#FF565656" CornerRadius="0,3,3,0" Width="Auto" Height="{Binding ElementName=bdrButtonStyle2,Path=Height}" Margin="2,0,0,0" Padding="5,0,0,0">
                            <TextBlock Foreground="White" VerticalAlignment="Center">
                               <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
                            </TextBlock>
                        </Border>

                    </BulletDecorator>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="TickMark" Property="Visibility" Value="Visible"/>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type RadioButton}" x:Key="buttonStyle3">
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontWeight" Value="DemiBold" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <BulletDecorator Cursor="Hand">

                        <Border CornerRadius="3" BorderBrush="#FFE2E2E2" BorderThickness="1" Padding="5,0,10,0">

                            <StackPanel Orientation="Horizontal">

                                <Ellipse Name="RadioMark" Width="20" Height="20" Margin="0,0,10,0" Fill="White"/>

                                <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />

                            </StackPanel>

                            <Border.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="White" Offset="0" />
                                    <GradientStop Color="#FFE2E2E2" Offset="1" />
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>

                    </BulletDecorator>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="RadioMark" Property="Fill" Value="Gold"/>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type RadioButton}" x:Key="buttonStyle4">
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontWeight" Value="DemiBold" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <BulletDecorator Cursor="Hand">

                        <Border CornerRadius="3" BorderBrush="#FFE2E2E2" BorderThickness="1" Padding="5,0,10,0">

                            <StackPanel Orientation="Horizontal">

                                <Ellipse Name="RadioMark" Width="20" Height="20" Margin="0,0,10,0" Stroke="White" Fill="White" StrokeThickness="7"/>

                                <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />

                            </StackPanel>

                            <Border.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="White" Offset="0" />
                                    <GradientStop Color="#FFE2E2E2" Offset="1" />
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>

                    </BulletDecorator>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="RadioMark" Property="Fill" Value="#8d5f00"/>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type RadioButton}" x:Key="buttonStyle5">
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontWeight" Value="DemiBold" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <BulletDecorator Cursor="Hand">

                        <Border CornerRadius="3" BorderThickness="1" Padding="5,0,10,0">

                            <StackPanel Orientation="Horizontal">

                                <Ellipse Name="RadioMark" Width="20" Height="20" Margin="0,0,5,0" Stroke="LightGray" Fill="White" StrokeThickness="7"/>

                                <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />

                            </StackPanel>
                        </Border>

                    </BulletDecorator>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="RadioMark" Property="Fill" Value="Black"/>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</ResourceDictionary>

Expected Output


External Resources

Windows WPF Autocomplete Textbox User Control

Introduction

This article is a simple approach for achieving reusable AutoComplete user control.

Skeleton of the XAML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<UserControl x:Class="AutocompleteTextbox.AutoCompliteTxtBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              Height="Auto"
             Width="Auto"
             Padding="0"
             x:Name="uc"
             Background="Transparent"
             Loaded="UserControl_Loaded">

    <StackPanel>
        <TextBox Grid.Column="0"
                 Grid.Row="0"
                 Name="textBox1"
                 Width="{Binding ElementName=uc,Path=Width}"
                 Height="{Binding ElementName=uc,Path=Height}"
                 Margin="{Binding ElementName=uc,Path=Margin}"
                 Padding="{Binding ElementName=uc,Path=Padding}"
                 IsEnabled="{Binding ElementName=uc,Path=IsEnabled}"
                 IsReadOnly="{Binding ElementName=uc,Path=IsReadOnly}"
                 HorizontalAlignment="{Binding ElementName=uc,Path=HorizontalAlignment}"
                 VerticalAlignment="{Binding ElementName=uc,Path=VerticalAlignment}"
                 TextChanged="textBox1_TextChanged"/>

        <Popup Name="myPopup"
               PlacementTarget="{Binding ElementName=textBox1}"
               MinWidth="{Binding ElementName=textBox1,Path=Width}"
               MinHeight="150"
               MaxHeight="500"
               AllowsTransparency="True"
               Focusable="True"
               StaysOpen="True"
               PopupAnimation="Slide"
               Placement="Bottom">

            <Popup.Style>
                <Style TargetType="{x:Type Popup}">
                    <Style.Triggers>

                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=textBox1, Path=IsFocused}" Value="True" />
                                <Condition Binding="{Binding ElementName=listBox1, Path=HasItems}" Value="true" />
                            </MultiDataTrigger.Conditions>

                            <MultiDataTrigger.Setters>
                                <Setter Property="IsOpen" Value="True" />
                                <Setter Property="StaysOpen" Value="True" />
                            </MultiDataTrigger.Setters>

                        </MultiDataTrigger>

                        <DataTrigger Binding="{Binding ElementName=textBox1, Path=IsFocused}" Value="False">
                            <Setter Property="IsOpen" Value="False" />
                            <Setter Property="StaysOpen" Value="False" />
                        </DataTrigger>

                        <DataTrigger Binding="{Binding ElementName=textBox1, Path=Text.Length}" Value="0">
                            <Setter Property="IsOpen" Value="False" />
                            <Setter Property="StaysOpen" Value="False" />
                        </DataTrigger>

                    </Style.Triggers>
                </Style>
            </Popup.Style>

            <ListBox Name="listBox1"
                     MinHeight="100"
                     Background="LightYellow"
                     HorizontalAlignment="Stretch"
                     SelectionMode="Single"
                     ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                     SelectionChanged="listBox1_SelectionChanged"/>
        </Popup>

    </StackPanel>

</UserControl>

Expected Output




External Resources