I want to use a VisualBrush
representing a hatching in a VisualState
to change the Fill
property of a polygon from a dependency property of type boolean
in my user control. Is it possible to do that ?
The change callback of this dependency property calls the method GoToState
of the VisualStateManager: VisualStateManager.GoToState(this, "MyVisualState", true);
The usercontrol is defined as shown below:
<UserControl x:Class="Test.MyUserControl"
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"
xmlns:local="clr-namespace:Test"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<VisualBrush x:Key="MyVisualBrush" TileMode="Tile" Viewport="0,0,15,15" ViewportUnits="Absolute" Viewbox="0,0,15,15" ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Grid Background="Gray">
<Path Data="M 0 15 L 15 0" Stroke="Black" StrokeThickness="2"/>
<Path Data="M 0 0 L 15 15" Stroke="Black" StrokeThickness="2" />
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</UserControl.Resources>
<Canvas>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup">
<VisualState x:Name="MyVisualState">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill)" Storyboard.TargetName="myPolygon">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MyVisualBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Polygon x:Name="myPolygon" Points="0,0 50,0 50,100 100,100 100,150 0,150" Fill="Gray" StrokeThickness="2" Stroke="Black"/>
</Canvas>
</UserControl>
When the method GoToState
is called, I get the error: "This Freezable cannot be frozen."
Can you explain me how to solve this ?
A VisualBrush
with a Visual
cannot be frozen.
You could set the Fill
property programmatically or use a Style
with a DataTrigger
that binds to a source property, e.g.:
<Polygon x:Name="myPolygon" Points="0,0 50,0 50,100 100,100 100,150 0,150" StrokeThickness="2" Stroke="Black">
<Polygon.Style>
<Style TargetType="Polygon">
<Setter Property="Fill" Value="Gray" />
<Style.Triggers>
<DataTrigger Binding="{Binding SomeProperty}" Value="True">
<Setter Property="Fill">
<Setter.Value>
<VisualBrush TileMode="Tile" Viewport="0,0,15,15" ViewportUnits="Absolute" Viewbox="0,0,15,15" ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Grid Background="Red">
<Path Data="M 0 15 L 15 0" Stroke="Black" StrokeThickness="2"/>
<Path Data="M 0 0 L 15 15" Stroke="Black" StrokeThickness="2" />
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Polygon.Style>
</Polygon>