Skip to content

Instantly share code, notes, and snippets.

@Vaccano
Last active December 15, 2015 00:29
Show Gist options
  • Save Vaccano/5173613 to your computer and use it in GitHub Desktop.
Save Vaccano/5173613 to your computer and use it in GitHub Desktop.
Custom Control for Stack Overflow Question
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Chromes="clr-namespace:Xceed.Wpf.Toolkit.Chromes;assembly=WPFToolkit.Extended"
xmlns:Controls="clr-namespace:OMStudio.Wpf.Controls"
xmlns:utils="clr-namespace:Arup.Wpf.Utils.Converters;assembly=Arup.Wpf.Utils"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
>
<xctk:InverseBoolConverter x:Key="InverseBoolConverter" />
<xctk:VisibilityToBoolConverter x:Key="VisibilityToBoolConverter" Inverted="True" />
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<utils:InverseOrientationConverter x:Key="InverseOrientationConverter" />
<Image x:Key="DefaultContent"
Width="18"
Height="16"
Source="/OMStudio.Wpf.Controls;component/Images/Notes16.png"
x:Shared="False"
/>
<LinearGradientBrush x:Key="PopupDarkBorderBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#FFA3AEB9" />
<GradientStop Offset="0.375" Color="#FF8399A9" />
<GradientStop Offset="0.375" Color="#FF718597" />
<GradientStop Offset="1" Color="#FF617584" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="PopupBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0" Color="#FFEBEBEB" />
<GradientStop Offset="1" Color="#FFCED4D8" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
<Style x:Key="ToggleButtonStyle" TargetType="ToggleButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid SnapsToDevicePixels="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="True"
>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
/>
</Border>
<Chromes:ButtonChrome x:Name="ToggleButtonChrome"
Grid.Column="1"
CornerRadius="0,2.75,2.75,0"
RenderChecked="{Binding IsOpen,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Controls:ComboPop}}"
RenderEnabled="{Binding IsEnabled,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Controls:ComboPop}}"
RenderMouseOver="{TemplateBinding IsMouseOver}"
RenderPressed="{TemplateBinding IsPressed}"
Visibility="{Binding ShowArrowButton,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Controls:ComboPop},
Converter={StaticResource BooleanToVisibilityConverter}}"
>
<Grid x:Name="arrowGlyph"
Grid.Column="1"
Margin="5"
IsHitTestVisible="False"
>
<Path x:Name="Arrow"
Width="7"
Height="4"
Data="M 0,1 C0,1 0,0 0,0 0,0 3,0 3,0 3,0 3,1 3,1 3,1 4,1 4,1 4,1 4,0 4,0 4,0 7,0 7,0 7,0 7,1 7,1 7,1 6,1 6,1 6,1 6,2 6,2 6,2 5,2 5,2 5,2 5,3 5,3 5,3 4,3 4,3 4,3 4,4 4,4 4,4 3,4 3,4 3,4 3,3 3,3 3,3 2,3 2,3 2,3 2,2 2,2 2,2 1,2 1,2 1,2 1,1 1,1 1,1 0,1 0,1 z"
Fill="#FF000000"
/>
</Grid>
</Chromes:ButtonChrome>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Arrow" Property="Fill" Value="#AFAFAF" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type Controls:ComboPop}">
<Setter Property="Background" Value="White" />
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#FFA3AEB9" />
<GradientStop Offset="0.375" Color="#FF8399A9" />
<GradientStop Offset="0.375" Color="#FF718597" />
<GradientStop Offset="1" Color="#FF617584" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="LabelTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Focusable="False" Orientation="Horizontal">
<Label VerticalAlignment="Center"
Content="{Binding}"
Focusable="False"
Padding="0"
/>
<TextBlock Margin="5,0,0,0"
VerticalAlignment="Center"
Focusable="False"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Controls:ComboPop}},
Path=RequiredIndicatorBrush}"
Text="*"
Visibility="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Controls:ComboPop}},
Path=IsRequired,
Converter={StaticResource BooleanToVisibilityConverter}}"
/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="1,1,0,1" />
<Setter Property="Content" Value="{StaticResource DefaultContent}" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Padding" Value="1" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:ComboPop}">
<Grid x:Name="Root">
<StackPanel x:Name="stackPanel"
VerticalAlignment="Top"
Orientation="{TemplateBinding Orientation,
Converter={StaticResource InverseOrientationConverter}}"
>
<Label x:Name="PART_LabelHost"
Width="{TemplateBinding LabelWidth}"
Margin="{TemplateBinding LabelMargin}"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Content="{TemplateBinding Label}"
ContentTemplate="{TemplateBinding LabelTemplate}"
IsHitTestVisible="False"
Padding="0"
Target="{Binding ElementName=PART_DropDownButton}"
Visibility="{TemplateBinding ShowLabel,
Converter={StaticResource BooleanToVisibilityConverter}}"
/>
<ToggleButton x:Name="PART_DropDownButton"
MinHeight="{TemplateBinding MinHeight}"
Height="{TemplateBinding ComboBoxHeight}"
Margin="0,2,0,0"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
IsChecked="{Binding IsOpen,
RelativeSource={RelativeSource TemplatedParent}}"
IsHitTestVisible="{Binding IsOpen,
RelativeSource={RelativeSource TemplatedParent},
Converter={StaticResource InverseBoolConverter}}"
IsTabStop="True"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="True"
Style="{StaticResource ToggleButtonStyle}"
/>
</StackPanel>
<Controls:ExtendedPopup x:Name="PART_Popup"
Width="{TemplateBinding DropDownWidth}"
Height="{TemplateBinding DropDownHeight}"
MaxHeight="{TemplateBinding DropDownMaxHeight}"
MaxWidth="{TemplateBinding DropDownMaxWidth}"
MinHeight="{TemplateBinding DropDownMinHeight}"
MinWidth="{TemplateBinding DropDownMinWidth}"
AllowsTransparency="True"
FocusManager.IsFocusScope="True"
AutoFocusOnOpened="{TemplateBinding AutoFocusOnOpened}"
IsOpen="{Binding IsChecked,
ElementName=PART_DropDownButton}"
Placement="Bottom"
PopupAnimation="Slide"
SnapsToDevicePixels="True"
StaysOpen="False"
>
<Border Background="{StaticResource PopupBackgroundBrush}"
BorderBrush="{StaticResource PopupDarkBorderBrush}"
BorderThickness="1"
>
<Grid>
<ContentControl x:Name="PART_Container"
Content="{TemplateBinding DropDownContent}"
Focusable="False"
/>
<TextBox x:Name="PART_LastTabStop"
Width="1"
Opacity="0"
/>
<Thumb x:Name="PART_ResizeThumb"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Cursor="SizeNWSE"
Visibility="{TemplateBinding IsResizeable,
Converter={StaticResource VisibilityToBoolConverter}}"
>
<Thumb.Template>
<ControlTemplate TargetType="{x:Type Thumb}">
<Grid Background="Transparent">
<Path Data="M0.5,6.5 L6.5,0.5 M6.5,3.5 L3.5,6.5"
Stroke="Black"
StrokeThickness="1"
/>
</Grid>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Grid>
</Border>
</Controls:ExtendedPopup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment