有没有办法对属性的属性使用样式设置器? [英] Is there a way to use a style setter for properties of properties?

查看:20
本文介绍了有没有办法对属性的属性使用样式设置器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最初的问题中,我对 setter 的工作方式做了一些错误的假设,因此我对其进行了修改,希望能更准确和有用.

如果鼠标未悬停在项目上,我试图通过使图标显示为半透明来使某些菜单项更有趣.如果鼠标进入,图标应该被动画化以完全可见.动画有效,Storyboard.TargetProperty 允许直接访问图标的不透明度属性:

I have tried to make some menu items more interesting by having the icons appear half-transparent if the mouse is not over the item. If the mouse enters, the icon should be animated to become completely visible. The animations work, Storyboard.TargetProperty allows direct access to the icon's opacity property:

<Style x:Key="MenuItemMouseOverStyle" TargetType="MenuItem">
    <Style.Triggers>
        <EventTrigger RoutedEvent="MouseEnter">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Icon.Opacity">
                        <EasingDoubleKeyFrame KeyTime="0" Value="0.5"/>
                        <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="MouseLeave">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Icon.Opacity">
                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                        <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.5"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>

如果我尝试对初始图标不透明度使用 setter,代码将无法编译:

If i try to use a setter for the initial icon-opacity the code won't compile:

<Setter Property="Icon.Opacity" Value="0.5"/>

<小时>

Setter 的工作方式与我尝试使用它们的方式不同,您无法访问属性的属性(请参阅答案)如果样式的目标类型还没有设置,你唯一能做的就是指定一个目标类,下面的样式应该是等效的:

<Style x:Key="Style1" TargetType="Image">
    <Setter Property="Opacity" Value="0.5"/>
</Style>
<Style x:Key="Style2">
    <Setter Property="Image.Opacity" Value="0.5"/>
</Style>

<小时>

所以我的问题是是否有办法让它以某种方式与 setter 一起使用.


So my question is if there is a way to make this somehow work with a setter.

(我目前的解决方法是使用 Loaded 事件触发的单关键帧故事板,效果很好)

(My current work-around is a single-keyframe storyboard that is triggered with the Loaded event which works quite well)

推荐答案

我认为您不能像那样访问属性的属性,因此转换本身不是问题.即使 Icon 是类型 Image 仍然不起作用.例如,您可以尝试使用网格的背景不透明度.Background 是 Grid 的依赖属性,Opacity 是 Brush 的依赖属性,但以下行不起作用

I don't think you can access a Property of a Property like that so the casting itself isn't the problem. Even if Icon was of Type Image that still wouldn't work. You can try with Backgrounds Opacity for a Grid for example. Background is a Dependency Property for Grid and Opacity is a Dependency Property for Brush but the following line won't work

<Grid Background.Opacity="0.8"/>

你会得到一个错误提示

可附加属性Opacity"是在背景"类型中找不到.

The attachable property 'Opacity' was not found in type 'Background'.

你必须像这样在背景中设置它

You would have to set this in the Background itself like this

<Grid>
    <Grid.Background>
        <SolidColorBrush Opacity="0.8"/>
    </Grid.Background>
</Grid>

当你做这样的事情时,这意味着什么

So what this means as when you do something like this

<Grid TextBlock.Foreground="Red">
    <TextBlock Text="Test"/>
</Grid>

您实际上是在为 TextBlock 使用附加属性前景.

you're actually using the Attached Property Foreground for TextBlock.

图像没有名为 Opacity 的附加属性,因此您也不能这样做

Image doesn't have an Attached Property called Opacity so you can't do this either

<MenuItem Image.Opacity="0.8" />

除了您已经在做的之外,另一种解决方法是使用类似的东西(最顶层的 MenuItem 或您想使用的任何地方).

Another workaround besides the one you're already doing is to use something like this (top-most MenuItem or wherever you want to use it).

<MenuItem x:Name="topMenuItem"
          ...>
    <MenuItem.Resources>
        <Style TargetType="Image">
            <Setter Property="Opacity" Value="0.5"/>
        </Style>
    </MenuItem.Resources>
    <!-- ... -->
</MenuItem>

这篇关于有没有办法对属性的属性使用样式设置器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆