我有一个WPF项目,我正在使用自定义标题栏。在制作最小化/最大化按钮时,我发现了一些对我来说没有多大意义的东西。当我在控件上显式设置Visibility
属性时,触发器不再能够更新该属性。我已经验证了使用WPF Inspector正确触发了触发器,当我没有明确设置Visibility
属性而是让它从触发器中获取值时,它可以正常工作。 / p>
我知道我不能明确地设置属性并且有一个可行的解决方案;然而,这似乎违背了我对触发器应该如何运作的了解。实际上,我正在寻找一个解释,说明它为什么不起作用,如果可能的话,在明确设置属性时如何使其工作。
这是我完整的WPF示例应用程序,它非常模仿我尝试做的事情。当Collapsed
从Visible
转到WindowState
时,Maximized
按钮应取代Normal
按钮,反之亦然。最后一列上的按钮是在未明确设置属性的情况下工作的按钮。
<Window x:Class="ButtonVisibilityTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ButtonVisibilityTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="btn" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="Transparent">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="#E6E6E6">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="#C6C6C6">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="48" />
<ColumnDefinition Width="48" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Column="1" Foreground="#000000">
<Grid Width="8" Height="8">
<Rectangle Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}" StrokeThickness="1" Width="9" Height="9"/>
</Grid>
<Button.Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource btn}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Normal">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<!-- Doesn't Work -->
<Button Grid.Column="1" Foreground="#000000" Visibility="Collapsed">
<Grid Width="8" Height="8">
<Rectangle Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}" StrokeThickness="1" Width="9" Height="1"/>
</Grid>
<Button.Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource btn}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Normal">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<!-- Works -->
<Button Grid.Column="2" Foreground="#000000">
<Grid Width="8" Height="8">
<Rectangle Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}" StrokeThickness="1" Width="9" Height="1"/>
</Grid>
<Button.Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource btn}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Normal">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>