简体   繁体   中英

Accessing a Transform from a UserControl's Style

I've got some XAML for a UserControl that looks roughly like this:

<UserControl>
    <UserControl.RenderTransform>
        <ScaleTransform ScaleX="1" ScaleY="1" />
    </UserControl.RenderTransform>
    <UserControl.Style>
        <Style TargetType="UserControl">
            <Style.Triggers>
                <DataTrigger Binding="..." Value="...">
                    <Setter Property="RenderTransform.ScaleX" Value="0.5" />
                    <Setter Property="RenderTransform.ScaleY" Value="0.5" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>
</UserControl>

But when I compile, I get the error:

Cannot resolve the Style Property 'ScaleX'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property.

I've tried all sorts of permutations of the Property but I can't find one that actually works. In other cases, I'll just name the ScaleTransform and reference that with TargetName. But you can't use TargetName in a Style Setter.

I guess my alternative is something like this:

<Setter Property="RenderTransform">
    <Setter.Value>
        <ScaleTransform ScaleX="0.5" ScaleY="0.5" />
    </Setter.Value>
</Setter>

But that seems a little heavy-handed.

I'm sure I'm just missing something. But I could really use some help here.

Setters do not allow property paths. If your scenario allows it switch out the whole transform with a new one. Alternatively you can use a single frame animation, which is even heavier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM