WPF 样式中的 Datatrigger:更改内容不起作用 [英] Datatrigger in WPF Style: Change content doesn't work
本文介绍了WPF 样式中的 Datatrigger:更改内容不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些自定义最大化 Button
的样式编码,但我遇到了一些问题.
I have some style coding for a custom maximize Button
and i'm getting some issues.
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderThickness="0">
<ContentPresenter Margin="10,7,10,7" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#01000000"/>
<Setter Property="Foreground" Value="#FFFFFFFF"/>
<Setter Property="Margin" Value="0"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#50FFFFFF"/>
</Trigger>
<!--This trigger fails-->
<DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource FindAncestor, AncestorType=acrylic:AcrylicWindow}}" Value="Maximized">
<Setter Property="Content">
<Setter.Value>
<Grid>
<Path Height="10" Width="10" Data="M2,0 L8,0 L8,6 M0,3 L6,3 M0,2 L6,2 L6,8 L0,8 Z"
Stroke="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"
StrokeThickness="1"
SnapsToDevicePixels="True"/>
</Grid>
</Setter.Value>
</Setter>
</DataTrigger>
<!--It Works Correctly-->
<DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource FindAncestor, AncestorType=acrylic:AcrylicWindow}}" Value="Normal">
<Setter Property="Content">
<Setter.Value>
<Grid>
<Path Width="10" Height="10" Data="F1M0,0L0,9 9,9 9,0 0,0 0,3 8,3 8,8 1,8 1,3z"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
SnapsToDevicePixels="True"/>
</Grid>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
当 Window
最大化时 Button
内容消失,它不显示路径,它是空的(但可点击并且 InMouseOver 中的淡入淡出有效).当 Window
处于正常状态时,按钮内容是正确的.我试图更改路径数据,但没有看到任何更改.
When the Window
is maximized the Button
content disappears, it doesn't show the path, it's empty (but clickable and the fade in InMouseOver works). When the Window
is in normal state the button content is correct.
I was trying to change the path data but i didn't see any change.
推荐答案
尝试将内容定义为资源:
Try to define the content as a resource:
<Style TargetType="Button">
<Style.Resources>
<Grid x:Key="max">
<Path Height="10" Width="10" Data="M2,0 L8,0 L8,6 M0,3 L6,3 M0,2 L6,2 L6,8 L0,8 Z"
Stroke="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"
StrokeThickness="1"
SnapsToDevicePixels="True"/>
</Grid>
<Grid x:Key="normal">
<Path Width="10" Height="10" Data="F1M0,0L0,9 9,9 9,0 0,0 0,3 8,3 8,8 1,8 1,3z"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
SnapsToDevicePixels="True"/>
</Grid>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderThickness="0">
<ContentPresenter Margin="10,7,10,7" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#01000000"/>
<Setter Property="Foreground" Value="#FFFFFFFF"/>
<Setter Property="Margin" Value="0"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#50FFFFFF"/>
</Trigger>
<DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Value="Maximized">
<Setter Property="Content" Value="{StaticResource max}" />
</DataTrigger>
<DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Value="Normal">
<Setter Property="Content" Value="{StaticResource normal}" />
</DataTrigger>
</Style.Triggers>
</Style>
这篇关于WPF 样式中的 Datatrigger:更改内容不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文