XAML背景颜色黑色在运行时

时间:2014-07-11 18:37:57

标签: c# wpf xaml

我正在构建一个WPF应用程序,并将窗口背景设置为" Wheat"但是当我运行应用程序时,背景是黑色的。任何人都可以对正在发生的事情有所了解吗?

设计时间

Design time

运行时间

Run time

这是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>

1 个答案:

答案 0 :(得分:1)

有问题的代码是边框样式的HorizontalVertical对齐方式。如果未指定Key,则将其用作TargetType的默认样式,因此它将应用于项目中的所有Borders。这将得到与您的代码相同的结果:

<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>

问题是,Window的样式使用Borders作为Control Template。使用Blend,您可以编辑应用模板的副本,它将为您提供:

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

模板的边框将拾取您的样式并将其应用于其内部Border。因此,要解决您的问题,请为您设置水平和垂直对齐的窗口应用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>

或将x:Key应用于边框样式,并将其添加到要使用该样式的边框

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