我为自定义最大化Button
使用了一些样式编码,但遇到了一些问题。
<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
处于正常状态时,按钮内容正确。
我试图更改路径数据,但没有看到任何更改。
答案 0 :(得分:1)
尝试将内容定义为资源:
<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>
答案 1 :(得分:1)
罪魁祸首是这条线:
Stroke="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}"
之所以这样,是因为实例化setter时无法解析此绑定,因为Path
尚不存在。
使用时
Stroke="#FFFFFFFF"
相反,它将起作用。
定义资源中的颜色并使用类似的
Stroke="{StaticResource ButtonForegroundColor}"
提高XAML代码的可维护性。