[英]WPF Button DataTrigger not setting Content correctly
I'm making a class, inherting from Window
which adds some additional functionality to the TitleBar.我正在制作一个 class,继承自
Window
,它为 TitleBar 添加了一些额外的功能。
I have tried to recreate the Windows 10 title bar using this XAML as the minimize, restore/maximize and close buttons:我尝试使用此 XAML 作为最小化、恢复/最大化和关闭按钮来重新创建 Windows 10 标题栏:
<Button x:Name="CloseButton"
Click="CloseButton_Click"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="" FontFamily="Segoe MDL2 Assets" FontSize="10" Padding="15,10" Background="Transparent" BorderBrush="Transparent" Foreground="White"
DockPanel.Dock="Right">
<Button.Style>
<Style TargetType="{x:Type Button}"/>
</Button.Style>
</Button>
<Button x:Name="MaximizeButton"
Click="MaximizeButton_Click"
DockPanel.Dock="Right"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Segoe MDL2 Assets" FontSize="10" Padding="15,10" Background="Transparent" BorderBrush="Transparent" Foreground="White">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Button.Content" Value="" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized">
<Setter Property="Button.Content" Value="" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MainWindow}}, Path=WindowState}" Value="Normal">
<Setter Property="Button.Content" Value="" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button x:Name="MinimizeButton"
Content=""
Click="MinimizeButton_Click"
DockPanel.Dock="Right"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Segoe MDL2 Assets" FontSize="10" Padding="15,15,15,5" Background="Transparent" BorderBrush="Transparent" Foreground="White">
<Button.Style>
<Style TargetType="{x:Type Button}"/>
</Button.Style>
</Button>
The problem is that while my Maximize button correctly displays like this, initially:问题是,虽然我的最大化按钮正确显示是这样的,但最初:
If I click it, it turns into this:如果我点击它,它会变成这样:
Which looks like a sort of invalid character?哪个看起来像一种无效字符? It is a valid character according to Segoe UI MDL2 icon guidelines .
根据Segoe UI MDL2 图标指南,它是一个有效字符。
If I then click it again to restore it, the restore works correctly but the icon stays the same and doesn't change back to the correct image, this:如果我然后再次单击它来恢复它,恢复工作正常,但图标保持不变并且不会变回正确的图像,这个:
Is there something wrong with my Style
or DataTrigger
?我的
Style
或DataTrigger
有问题吗?
You'll note I've tried AncestorType={x:Type Window}}
and AncestorType={x:Type local:MainWindow}}
, MainWindow
being my class, but neither of these made any difference.你会注意到我已经尝试过
AncestorType={x:Type Window}}
和AncestorType={x:Type local:MainWindow}}
, MainWindow
是我的 class,但这些都没有任何区别。
I realised I was setting the Button.Content
in the MaximizeButton_Click
method, which I'd forgotten about.我意识到我在
MaximizeButton_Click
方法中设置了Button.Content
,但我已经忘记了。 Removing that and it all works as expected.删除它,这一切都按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.