【问题标题】:WPF windows background colorWPF 窗口背景颜色
【发布时间】:2019-04-11 03:11:33
【问题描述】:

默认情况下 wpf 窗口具有白色。所以我应该为背景指定什么颜色,结果它看起来像普通窗口,就像 .net 2.0 win 应用程序窗口颜色一样。请帮忙

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您需要使用系统颜色画笔绘制背景。

    SystemColors.ControlBrushKey property 将为相应的SolidColorBrush 返回ResourceKey

    例如,要设置按钮的背景,您可以使用以下代码:

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      WindowTitle="SystemColors Example" Background="White">  
      <StackPanel Margin="20">
        <Button 
          Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" 
          Content="Hello, World!" />
      </StackPanel>
    </Page>
    

    【讨论】:

    • 为什么要在这里使用 DynamicResource 而不是 StaticResource?另外,为什么 Visual Studio 2012 的 Intellisense 不能识别“x:Static”?
    • @BlueMonkMN:请参考docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/…:“但是,不推荐对已知仅存在于主题中或作为系统资源的键的静态资源引用。这是因为不会重新评估此类引用如果用户实时更改主题。当您请求主题或系统资源时,动态资源引用更可靠。”
    【解决方案2】:

    将窗口的背景动态绑定到系统调色板中的“控制”画笔。你可以在你的窗口类的构造函数中这样做:

    SetResourceReference(BackgroundProperty, SystemColors.ControlBrushKey);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多