WPF 样式.触发器
有人可以告诉我为什么这个触发器不起作用:
<!--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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将样式应用到 Expander,如下所示:
如果您不想显式应用样式,则不要声明
x:Key="Test"
,并且所有 Expander 都在同一范围内因为该资源(即,如果您在
中声明了x:Key="Test"
,则该特定页面中的所有 Expander)将继承该样式。You need to apply the style to the Expander like:
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 declaredx:Key="Test"
in<Page.Resources>
) will inherit that style.谢谢,但我忘记了你提到的代码块。我在样式标签中添加了引用,并且问题仍然存在。
Thanks, but i forgot the block of code that you mentioned. I put a reference in Style tag, and, the problem persist.
问题在于,当用户单击扩展器时,会设置 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 ifIsExpanded
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.