Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Last active December 19, 2015 15:09
Show Gist options
  • Save JohanLarsson/5974239 to your computer and use it in GitHub Desktop.
Save JohanLarsson/5974239 to your computer and use it in GitHub Desktop.
public static class AttachedProperties
{
public static readonly DependencyProperty IconProperty = DependencyProperty.RegisterAttached(
"Icon",
typeof (StreamGeometry),
typeof (AttachedProperties),
new PropertyMetadata(default(StreamGeometry))
);
public static void SetIcon(UIElement element, StreamGeometry value)
{
element.SetValue(IconProperty, value);
}
public static StreamGeometry GetIcon(UIElement element)
{
return (StreamGeometry)element.GetValue(IconProperty);
}
}
<Window x:Class="ImagesPanelTemp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ImagesPanelTemp"
Title="MainWindow" SizeToContent="WidthAndHeight">
<Window.Resources>
<Style x:Key="ImageButton" TargetType="{x:Type Button}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Path Data="{Binding Path=(local:AttachedProperties.Icon), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"
HorizontalAlignment="Left" Margin="8,0,0,0" Height="100" Width="100" Fill="Blue" Stretch="UniformToFill" Stroke="Red" StrokeThickness="6"/>
<TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" />
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Content="Test" local:AttachedProperties.Icon="M1,0 1,1 0,1 z" Style="{StaticResource ImageButton}"/>
</Grid>
</Window>
<Button Content="Pressing" local:AttachedProperties.Icon="{StaticResource Picka}" Style="{StaticResource ImageButton}"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment