简体   繁体   English

在 XAML 中投射 WPF 属性

[英]Cast WPF property in XAML

I'm wondering if it's possible to cast properties in WPF?我想知道是否可以在 WPF 中投射属性?

I've this code我有这个代码

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="(Window.Opacity)" From="0.0" To="0.92" Duration="0:0:0.15" AutoReverse="False" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>

and i want to modify my window background opacity but i need to cast Window.Background to SolidColorBrush that's why i'm wondering if it's possible and if yes how to do it (and if not if you have a good way to do it)我想修改我的 window 背景不透明度,但我需要将 Window.Background 转换为 SolidColorBrush 这就是为什么我想知道它是否可能以及如果是的话怎么做(如果没有的话,如果你有一个好的方法)

Thanks for your help:)谢谢你的帮助:)

This will work, as a replacement for the DoubleAnimation in your question.这将起作用,作为您问题中 DoubleAnimation 的替代品。 You were very close.你非常亲近。 There's usually no need for type casting in XAML. XAML 通常不需要类型转换。 It's "duck typed".它是“鸭式”。 However, lankitha's excellent answer illustrates how it's done in a property path: (Window.Background).(SolidColorBrush.Color) .但是,lankitha 的出色回答说明了它是如何在属性路径中完成的: (Window.Background).(SolidColorBrush.Color) Or (Window.Background).(SolidColorBrush.Opacity) here.(Window.Background).(SolidColorBrush.Opacity)在这里。 But that's not necessary in this case.但在这种情况下,这不是必需的。

<DoubleAnimation
    Storyboard.TargetProperty="Background.Opacity"
    From="0.0" 
    To="0.92" 
    Duration="0:0:0.15" 
    AutoReverse="False" 
    />

If you want to make your window to be actually transparent by making the background transparent, you'll need the following as well on the Window element itself:如果您想通过使背景透明来使您的 window 实际上透明,您还需要在 Window 元素本身上进行以下操作:

AllowsTransparency="True"
WindowStyle="None"

Please try ColorAnimation functionality.请尝试ColorAnimation功能。 Usually, #00000000 stands for transparent a hex colour with additional two zeros for the opacity at the start, in the format of aRGB .通常,#00000000 代表透明的十六进制颜色,开头的不透明度有两个零,格式为aRGB Set the colour of the window to the colour mentioned above.将 window 的颜色设置为上述颜色。 and in the colour animation set it to the desired colour.并在颜色 animation 中将其设置为所需的颜色。

try this尝试这个

<EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation To="#FF000000" 
                                    Storyboard.TargetProperty="(Window.Background).(SolidColorBrush.Color)" 
                                    FillBehavior="Stop" 
                                    Duration="0:0:0.15"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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