【问题标题】:Buttons looking old/weird on WPF by default默认情况下,在 WPF 上看起来很旧/很奇怪的按钮
【发布时间】:2021-10-17 02:41:51
【问题描述】:

出于某种原因,我计算机上 WPF 上的按钮在 VS2019 编辑器和应用程序内(无论是调试模式还是发布模式)看起来都很奇怪和陈旧。我尝试创建一个新项目/解决方案,将样式更改为“Style="{x:Null}",卸载更改我的 Windows 设计的应用程序(例如 open shell 或雨量计),但它们看起来仍然陈旧而奇怪我的电脑。 唯一有效的是在另一台计算机上测试我的应用程序,我的应用程序(在发布模式下)看起来应该,但它在我的计算机上仍然看起来很奇怪,并且让我很难预览应用程序的外观(并且还使该应用在我的 PC 上看起来很丑)。

我能做什么?

如果需要,这里是 XAML:

<Window x:Class="WPFApp1.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:WPFApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="100"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Button Content="My Button" Grid.Row="1" Grid.Column="1"/>
</Grid>

【问题讨论】:

  • 您还没有解释为什么按钮应该看起来不同。具有默认模板的按钮在不同计算机上看起来不同的事实可以通过以下事实来解释:所有元素的默认模板不是由 Windows 操作系统本身设置的,而是由这台特定计算机上使用的主题设置的。如果主题不同,则元素看起来可能不同。

标签: c# .net wpf windows visual-studio


【解决方案1】:

你没有展示它“应该是什么样子”,但如果你喜欢:

您可以执行以下操作:

MainWindow.xaml

<Window x:Class="WpfModernButton.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:WpfModernButton"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="100"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Button Grid.Row="1" Grid.Column="1" x:Name="button1"  VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" Height="100" BorderThickness="0" FontSize="22" Content="My Button">
            <Button.Background>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,2">
                    <GradientStop Color="#5bc3ff" Offset="0.0" />
                    <GradientStop Color="#3aa0ff" Offset="1" />
                </LinearGradientBrush>
            </Button.Background>

            <Button.Foreground>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,2">
                    <GradientStop Color="#ffffff" Offset="0.0" />
                </LinearGradientBrush>
               
            </Button.Foreground>

            <Button.Clip>
                <RectangleGeometry RadiusX="10"
                                   RadiusY="10"
                                   Rect="0,0,200, 100" />
            </Button.Clip>
        </Button>
    </Grid>
</Window>

资源

【讨论】:

  • 您的回答与TC提出的问题无关。
  • @EldHasp:我不同意。虽然描述相当模糊,但 OP 表示他/她的按钮看起来“又旧又奇怪”。显示的按钮图像是一个灰色的矩形按钮——这在过去很常见。我显示的按钮是蓝色的,带有圆角。如果你有更好的解决方案,有空间供你提供。如果你的解决方案很好,我可能会考虑自己使用它。
  • 从 TS 的解释中,我们可以得出结论,他期望他的按钮有不同的外观。他的期望在另一台计算机上得到满足,但在他的计算机上却没有。由于他没有同时对程序进行任何更改,因此可以推断他的应用程序使用主题设置的默认元素类型是合乎逻辑的。如果它使用您的代码,它将破坏元素样式与 Windows 主题设置的样式的联系。
  • @EldHasp:我还在学习 WPF。 “它会破坏元素样式与 Windows 主题设置的样式之间的联系”是什么意思? OP 似乎对默认的 Windows 主题不满意。为了在机器和操作系统之间提供一致的体验,似乎需要忽略默认的 Windows 主题。
  • 我对 TC 问题的理解不同。两台电脑上安装了一个相同的主题。但在他的电脑上,元素不使用这个主题,而在另一台电脑上,他们使用它。但我不确定这个假设,所以我请他澄清“为什么他期望与计算机中的元素有不同的外观”。
猜你喜欢
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
  • 2013-02-24
相关资源
最近更新 更多