I’m trying to figure it out how to reuse a style (specifically the Data property) for a path that’s on a Resource Dictionary.
Here’s my problem:
ResourceDictionary.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Path" x:Key="FlashOn">
<Setter Property="Data">
<Setter.Value>
F1M376.251,632.755L385.665,632.755 381.302,646.07 394.618,646.07 389.11,660.302 393.01,660.302 381.531,672.93 377.398,660.763 381.073,660.763 383.829,652.268 369.825,652.268 376.251,632.755z
</Setter.Value>
</Setter>
<Setter Property="Fill" Value="#FFFFFF"></Setter>
<Setter Property="Stretch" Value="Fill"></Setter>
<Setter Property="Height" Value="25"></Setter>
<Setter Property="Width" Value="25"></Setter>
</Style>
</ResourceDictionary>
And trying to use it on a page like this:
<Path Style="{StaticResource FlashOn}"/>
or like this
System.Windows.Shapes.Path p = new System.Windows.Shapes.Path();
p.Style = Application.Current.Resources["FlashOn"] as Style;
The first time I load the page the shape will appear perfectly, BUT if I go back and then go again to that page the all the styles are available EXCEPT for the Data which does not have any information at all. So, I can stylize all the properties in a custom shape except for the Data property.
**Just to note, If the style is inline everything works. I just don’t want to repeat code if I use the same path several times in the app.
<Path Width="25" Height="25" Stretch="Fill" Fill="#FFFFFF" Data="F1M376.251,632.755L385.665,632.755 381.302,646.07 394.618,646.07 389.11,660.302 393.01,660.302 381.531,672.93 377.398,660.763 381.073,660.763 383.829,652.268 369.825,652.268 376.251,632.755z" />
No sweat, just turn it into a Content Control;
Then invoke it;
This does a couple things for you, like not making your app re-draw the Path every instance so optimizes things. It also allows you (if you wanted/needed to) to bind dependency properties like Width/Height/Alignment/Margin or whatever you need to the template so you can specify them inline if you need to or you can keep them hard coded values like you have them. Hope this helps.