wpf在样式中查找内部网格

发布于 2024-12-11 00:52:51 字数 1604 浏览 0 评论 0原文

我有一个在 xaml 中定义的按钮样式,里面有一个故事板和一个网格。故事板将网格的不透明度设置为 0.1。问题是如何通过代码恢复它。我想我可以访问网格并将不透明度设置为 1,但我找不到通过 C# 获取网格的方法。

这是代码:

xaml:

<Style x:Key="BevelWLButton" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}" x:Name="bbtemplate">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="FadeOut" >
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid">
                                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.05"/>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>

                    </ControlTemplate.Resources>
                    <Grid x:Name="grid">
                        -------------- up is the grid i find
    </Style>

按钮是由代码创建的,并且样式

在此处正确设置,我开始故事板:

var story = btn.Template.Resources["FadeOut"] as Storyboard;
                if (story != null)
                {
                    story = story.Clone();
                    story.Begin(btn,btn.Template);
                }

但是当我尝试时,

currentButton.Resources["grid"]; or currentButton.Template.Resources["grid"];

结果为空。所以,我无法重新建立不透明度

有什么想法吗?

提前致谢。

I have a button style defined in xaml, inside i have an storyboard and among other things a grid. The storyboard set the opacity of the grid to 0.1. The question is how to restore it via code. I think i can access the grid and set the opacity to 1 but i can not find a way to get the grid via c#.

Here is the code:

xaml:

<Style x:Key="BevelWLButton" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}" x:Name="bbtemplate">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="FadeOut" >
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid">
                                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.05"/>
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>

                    </ControlTemplate.Resources>
                    <Grid x:Name="grid">
                        -------------- up is the grid i find
    </Style>

the buttons are created by code and the style is setted properly

here i begin the storyboard:

var story = btn.Template.Resources["FadeOut"] as Storyboard;
                if (story != null)
                {
                    story = story.Clone();
                    story.Begin(btn,btn.Template);
                }

but when i try to

currentButton.Resources["grid"]; or currentButton.Template.Resources["grid"];

the result is null. So, i can not restablish opacity

Any Idea?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来世叙缘 2024-12-18 00:52:51

你可以这样做

Grid gridInTemplate = (Grid)currentButton.Template.FindName("grid", currentButton);

You can do this

Grid gridInTemplate = (Grid)currentButton.Template.FindName("grid", currentButton);
一影成城 2024-12-18 00:52:51

您是否尝试过 currentButton.Template.FindName("grid", currentButton) as Grid

Did you try currentButton.Template.FindName("grid", currentButton) as Grid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文