Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XAML运行时背景色为黑色_C#_Wpf_Xaml - Fatal编程技术网

C# XAML运行时背景色为黑色

C# XAML运行时背景色为黑色,c#,wpf,xaml,C#,Wpf,Xaml,我正在构建一个WPF应用程序,并将窗口背景设置为“小麦”,但当我运行应用程序时,背景是黑色的。有人能解释一下发生了什么事吗 设计时间 运行时 以下是XAML: <Window x:Class="App.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我正在构建一个WPF应用程序,并将窗口背景设置为“小麦”,但当我运行应用程序时,背景是黑色的。有人能解释一下发生了什么事吗

设计时间

运行时

以下是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>

数据库服务器/侦听器

问题代码是边框样式的水平对齐和垂直对齐。如果未指定关键点,则它将用作TargetType的默认样式,因此它将应用于项目中的所有边框。这将产生与代码相同的结果:

<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>
或者对边框样式应用
x:Key
,并将其添加到要使用该样式的边框中

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


可能是某个控件的内部布局使用了
边框(但无法确定是哪一个)。。请注意,如果为边框样式设置了
x:Key
,背景将变为白色。@默认情况下,将x:Key添加到边框并设置每个边框控件的样式可以解决此问题。感谢您的解释,这很有意义。
<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>
<Style x:Key="MyBorderStyle" TargetType="Border">
<Border Style="{StaticResource MyBorderStyle}">     
</Border>