在VisualState中使用VisualBrush,但不能冻结可冻结对象



我想使用代表VisualState中阴影的VisualBrush来更改多边形的Fill属性,而不是用户控件中类型为boolean的依赖属性。有可能做到吗?

此依赖属性的更改回调调用VisualStateManager的方法GoToStateVisualStateManager.GoToState(this, "MyVisualState", true);

用户控件定义如下:

<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>

当方法GoToState被调用时,我得到错误:;无法冻结此Freezable">

你能解释一下如何解决这个问题吗?

带有VisualVisualBrush不能冻结。

您可以通过编程设置Fill属性,也可以将Style与绑定到源属性的DataTrigger一起使用,例如:

<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>

相关内容

最新更新