简体   繁体   English

WPF 按钮数据触发器未正确设置内容

[英]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="&#xE106;" 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="&#xE739;" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized">
                                <Setter Property="Button.Content" Value="&#xE923;" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MainWindow}}, Path=WindowState}" Value="Normal">
                                <Setter Property="Button.Content" Value="&#xE739;" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>

            <Button x:Name="MinimizeButton"
                    Content="&#xE949;"
                    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 ?我的StyleDataTrigger有问题吗?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM