Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Nice one Chris. If you want to store the paths as resources, you can modify things a bit like so: </p> <p>First change the dependency property to a Path type:</p> <pre><code>/// &lt;summary&gt; /// Button that contains an icon, where the icon is drawn from a path. /// &lt;/summary&gt; public class IconButton : Button { /// &lt;summary&gt; /// The path data that draws the icon. /// &lt;/summary&gt; public static readonly DependencyProperty IconPathProperty = DependencyProperty.Register("IconPath", typeof(Path), typeof(IconButton), new PropertyMetadata(default(Path))); /// &lt;summary&gt; /// Depencendy property backer. /// &lt;/summary&gt; public Path IconPath { get { return (Path)GetValue(IconPathProperty); } set { SetValue(IconPathProperty, value); } } } </code></pre> <p>Next, here's the IconButton control template.</p> <pre><code>&lt;!-- Icon Button Control Template --&gt; &lt;ControlTemplate x:Key="IconButtonControlTemplate" TargetType="{x:Type usercontrols:IconButton}"&gt; &lt;Grid x:Name="Grid"&gt; &lt;Border SnapsToDevicePixels="True" x:Name="Border" CornerRadius="1" BorderThickness="1" BorderBrush="{StaticResource ButtonBorderBrush}"&gt; &lt;Path x:Name="Icon" Height="16" Width="16" Stretch="Fill" Data="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IconPath.Data}" UseLayoutRounding="False" Grid.Column="1" VerticalAlignment="Center" Margin="0"&gt; &lt;Path.Fill&gt; &lt;SolidColorBrush x:Name="IconColor" Color="{Binding Color, Source={StaticResource ButtonIconBrush}}" /&gt; &lt;/Path.Fill&gt; &lt;/Path&gt; &lt;/Border&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsKeyboardFocused" Value="true"&gt; &lt;Setter Property="BorderBrush" Value="{StaticResource ButtonBorderBrush}" TargetName="Border"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsMouseOver" Value="true"&gt; &lt;Setter Property="Background" Value="{StaticResource ButtonBackgroundMouseOverBrush}" TargetName="Border"/&gt; &lt;Setter Property="BorderBrush" Value="{StaticResource ButtonBorderMouseOverBrush}" TargetName="Border"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsPressed" Value="true"&gt; &lt;Setter Property="Background" Value="{StaticResource ButtonBackgroundPressedBrush}" TargetName="Border"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsEnabled" Value="false"&gt; &lt;Setter Property="Fill" Value="{StaticResource DisabledIconBrush}" TargetName="Icon"/&gt; &lt;Setter Property="Background" Value="{StaticResource ButtonBackgroundDisabledBrush}" TargetName="Border"/&gt; &lt;Setter Property="BorderBrush" Value="{StaticResource ButtonBorderDisabledBrush}" TargetName="Border"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; </code></pre> <p>... and here's the IconButton style that uses this template, and adds a few defaults:</p> <pre><code>&lt;!-- Icon Button Style --&gt; &lt;Style TargetType="{x:Type usercontrols:IconButton}"&gt; &lt;Setter Property="FocusVisualStyle" Value="{DynamicResource SimpleButtonFocusVisual}"/&gt; &lt;Setter Property="HorizontalAlignment" Value="Left"/&gt; &lt;Setter Property="VerticalAlignment" Value="Top"/&gt; &lt;Setter Property="Height" Value="26"/&gt; &lt;Setter Property="Width" Value="26"/&gt; &lt;Setter Property="Margin" Value="5"/&gt; &lt;Setter Property="Content" Value="Button"/&gt; &lt;Setter Property="Template" Value="{StaticResource IconButtonControlTemplate}"/&gt; &lt;/Style&gt; </code></pre> <p>Now you can go ahead and create icons and save them in your resource file: </p> <pre><code>&lt;!-- Undo Icon --&gt; &lt;Path x:Key="UndoIcon" Stretch="Fill" Data="F1 M 87.7743,80.7396L 87.5215,75.9539L 89.8692,74.2202C 89.9775,75.0329 90.0859,76.586 90.0859,76.5318C 92.9302,73.7417 97.5369,72.9755 100.208,76.2158C 102.019,78.413 102.258,81.2543 99.7657,83.9361C 97.2735,86.6179 92.6142,90.1124 92.6142,90.1124L 90.3748,87.6744L 97.3096,81.769C 97.3096,81.769 99.1516,79.9992 97.8514,78.3558C 96.2374,76.316 94.384,77.2542 92.1447,78.8795C 92.1176,78.9608 93.3998,79.1143 94.3118,79.2768L 92.4336,81.4439L 87.7743,80.7396 Z "/&gt; &lt;!-- Filter Icon --&gt; &lt;Path x:Key="FilterIcon" Stretch="Fill" Data="F1 M 6,16L 10,16L 10.0208,10L 16,3L 16,2.86102e-006L 0,9.53674e-007L 0,3L 6,10L 6,16 Z "/&gt; </code></pre> <p>And finally, create buttons:</p> <pre><code>&lt;usercontrols:IconButton IconPath="{StaticResource UndoIcon}"&gt;&lt;/usercontrols:IconButton&gt; </code></pre> <hr>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload