在 VisualState 中使用 VisualBrush 但无法冻结 freezable

Use a VisualBrush in VisualState but freezable cannot be frozen

我想使用 VisualBrush 表示 VisualState 中的影线,以从依赖项 属性 更改多边形的 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。”

你能告诉我如何解决这个问题吗?

无法冻结 VisualBrushVisual

您可以通过编程方式设置 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>