WPF 样式.触发器

发布于 2024-08-07 06:11:57 字数 979 浏览 10 评论 0原文

有人可以告诉我为什么这个触发器不起作用:

<!--Style-->
<Style x:Key="Test" TargetType="{x:Type Expander}">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="IsExpanded" Value="false"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

<!-- Syle applyed in expander -->
<Expander Header="Expander" Margin="40,89,118,0" Name="expander1" Height="23" VerticalAlignment="Top" >
            <Grid>
                <Ellipse Height="100" Margin="86,0,-8,-58" Name="ellipse1" Stroke="Black" VerticalAlignment="Bottom" />        
            </Grid>
        </Expander>

<!-- Code Behind -->
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.expander1.IsEnabled = false;
        }

我尝试了其他方法,但没有成功.. 禁用扩展器时有另一种折叠内容的方法。

Somebody can tell me why this trigger doesn't work:

<!--Style-->
<Style x:Key="Test" TargetType="{x:Type Expander}">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="IsExpanded" Value="false"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

<!-- Syle applyed in expander -->
<Expander Header="Expander" Margin="40,89,118,0" Name="expander1" Height="23" VerticalAlignment="Top" >
            <Grid>
                <Ellipse Height="100" Margin="86,0,-8,-58" Name="ellipse1" Stroke="Black" VerticalAlignment="Bottom" />        
            </Grid>
        </Expander>

<!-- Code Behind -->
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.expander1.IsEnabled = false;
        }

I try in other ways, but without success..
Have another way to collapse content when expander is disabled.

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

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

发布评论

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

评论(3

不知在何时 2024-08-14 06:11:57

您需要将样式应用到 Expander,如下所示:

<Expander Header="Expander" Name="expander1" Style="{StaticResource Test}" >
...
</Expander>

如果您不想显式应用样式,则不要声明 x:Key="Test",并且所有 Expander 都在同一范围内因为该资源(即,如果您在 中声明了 x:Key="Test",则该特定页面中的所有 Expander)将继承该样式。

You need to apply the style to the Expander like:

<Expander Header="Expander" Name="expander1" Style="{StaticResource Test}" >
...
</Expander>

If you don't want to explicitly apply the style, then don't declare x:Key="Test", and all Expanders in the same scope as that resource (i.e. all Expanders in that particular Page if you declared x:Key="Test" in <Page.Resources>) will inherit that style.

兰花执着 2024-08-14 06:11:57

谢谢,但我忘记了你提到的代码块。我在样式标签中添加了引用,并且问题仍然存在。

Thanks, but i forgot the block of code that you mentioned. I put a reference in Style tag, and, the problem persist.

内心激荡 2024-08-14 06:11:57

问题在于,当用户单击扩展器时,会设置 IsExpanded“本地值”,该值优先于触发器。有关依赖项属性值优先级的详细信息,请参阅此 msdn 页面

一个可能的解决方案是重写整个 Expander 控件模板,向模板的触发器添加一个触发器,如果​​ IsEnabled 为 false,则隐藏“已扩展”区域(默认模板仅在 IsExpanded 时执行此操作) 是假的)。您可以使用 Blend 获取默认模板,方法是在窗口上添加扩展器,右键单击它并选择“编辑模板”>“编辑模板”。在上下文菜单中编辑副本。

The problem is that when the user clicks on the expander, the IsExpanded "local value" is set, which takes precedence over the trigger. See this msdn page for more information about dependency property value precedence.

A possible solution will be to rewrite the entire Expander control template, adding a trigger to the template's triggers that hides the "expanded" zone if IsEnabled is false (the default template only does this if IsExpanded is false). You can get the default template using Blend, by adding an expander on a Window, right click on it and select Edit Template > Edit a copy in the contextual menu.

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