有没有一种方法可以将样式设置器用于属性的属性? [英] Is there a way to use a style setter for properties of properties?

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

问题描述

编辑: 在最初的问题中,我对二传手的工作方式做出了一些错误的假设,因此我对其进行了修改以希望更加准确和有用。

我试图使一些菜单项更加有趣,如果鼠标不在菜单上,则使图标显示为半透明。如果鼠标进入,则应设置图标动画以使其完全可见。
动画作品 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"/>






编辑:
设置器无法按我尝试使用的方式工作,您无法访问属性的属性(请参阅答案)
您唯一可以做的就是指定目标类(如果目标类型为样式尚未设置,以下样式应等效:

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






所以我的问题是是否有办法


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)

推荐答案

我认为您无法访问像这样的Property因此,投放本身并不是问题。即使Icon的图片类型仍然无效。例如,您可以尝试使用背景不透明度作为网格。背景是网格的依赖属性,不透明度是画笔的依赖属性,但以下行不起作用

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

您会得到一个错误提示


在背景类型中找不到附加属性不透明度为

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天全站免登陆