简体   繁体   English

XAML背景颜色在运行时为黑色

[英]XAML Background Color Black at Runtime

I am building a WPF application and have set the window background to a "Wheat" but when I run the application the background is black. 我正在构建WPF应用程序,并将窗口背景设置为“小麦”,但是当我运行该应用程序时,背景为黑色。 Can anyone shed some light on what's going on? 任何人都可以对发生的事情有所了解吗?

Design Time 设计时间

设计时间

Run Time 运行

运行

Here is the XAML: 这是XAML:

<Window x:Class="App.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Main Window" Height="350" Width="525"
    TextElement.FontFamily="Calibri"
    TextElement.FontSize="14"  
    Background="Wheat">
    <Window.Resources>
        <XmlDataProvider x:Key="dbInfo" Source="DBConnectionInfo.xml" XPath="dbConnectionInfo"/>
        <Style TargetType="Border">
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="CornerRadius" Value="4"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
        <Style TargetType="StackPanel" x:Key="mainStackPanel">            
            <Setter Property="Background" Value="Transparent"/>            
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="10"/>            
        </Style>
        <Style TargetType="StackPanel">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="10"/>
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Margin" Value="10,10,10,10"/>
        </Style>
        <Style TargetType="ComboBox">
            <Setter Property="Margin" Value="10,10,10,10"/>
        </Style>
        <Style TargetType="Button">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Padding" Value="10,5,10,5"/>
            <Setter Property="Margin" Value="5"/>
        </Style>
    </Window.Resources>
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>            
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Column="0" Grid.Row="0" Style="{StaticResource mainStackPanel}">
            <Grid Visibility="Visible" Name="loginGrid">
                <Border>
                    <StackPanel>
                        <TextBlock>Database Server / Listener</TextBlock>
                        <ComboBox   Name="hostComboBox"
                                    ItemsSource="{Binding Source={StaticResource dbInfo},
                                    XPath=./dbConnections/dbConnection}"
                                    DisplayMemberPath="@database"
                                    SelectedValuePath="@host"
                                    SelectedIndex="1"/>
                        <Button Name="loginButton" Click="loginButton_Click" Content="Open"/>
                    </StackPanel>
                </Border>
            </Grid>
            <Grid Visibility="Collapsed" Name="selectGrid">
                <Border>
                    <StackPanel Grid.Column="0" Grid.Row="0" Orientation="Vertical" Background="Transparent">
                        <Button Content="Forms"/>
                        <Button Content="Documents"/>
                    </StackPanel>
                </Border>
            </Grid>
        </StackPanel>
    </Grid>
</Window>

The problematic code was the Horizontal and Vertical alignment of the Style for the Border. 有问题的代码是边框样式的HorizontalVertical对齐。 When no Key is specified it is used as the default style for the TargetType, thus it will apply to all Borders in your project. 如果未指定任何Key,它将用作TargetType的默认样式,因此它将应用于您项目中的所有Borders。 This will give the same result as your code: 这将产生与您的代码相同的结果:

<Window 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"
        mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Background="Red">
    <Window.Resources>
        <Style TargetType="Border">
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </Window.Resources>
</Window>

The problem is, the style for the Window uses Borders for its Control Template . 问题是,窗口的样式使用边框作为其控制模板 Using Blend, you can edit a copy of the applied Template, which will give you: 使用Blend,您可以编辑已应用模板的副本,这将为您提供:

<ControlTemplate TargetType="{x:Type Window}">
    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
        <AdornerDecorator>
            <ContentPresenter/>
        </AdornerDecorator>
    </Border>
</ControlTemplate>

The border for the Template will pick up your style and apply it to its internal Border . 模板的边框将拾取您的样式并将其应用于其内部Border So, to resolve your issue, either apply a ControlTemplate for your Window where you set the Horizontal and Vertical Alignment: 因此,要解决您的问题,请为您的窗口应用一个ControlTemplate,在其中设置水平和垂直对齐方式:

<Window.Template>
    <ControlTemplate TargetType="{x:Type Window}">
        <Border BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Background="{TemplateBinding Background}"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch">
            <AdornerDecorator>
                <ContentPresenter/>
            </AdornerDecorator>
        </Border>
    </ControlTemplate>
</Window.Template>

or apply a x:Key to your border style and add that to the borders to which you want to use that style 或将x:Key应用于边框样式并将其添加到要使用该样式的边框

<Style x:Key="MyBorderStyle" TargetType="Border">
<Border Style="{StaticResource MyBorderStyle}">     
</Border>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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