2011-04-07 87 views
5

我創建了簡單的窗口和應用樣式。現在,當我運行的應用背景是黑色的: enter image description hereWPF窗口的黑色背景

在XAML它看起來正常: enter image description here

是什麼原因造成的?

這裏是XAML代碼:

<Window SizeToContent="WidthAndHeight" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        x:Class="WPFTest.MainWindow"> 
    <Window.Style> 
     <Style TargetType="Window"> 
      <Setter Property="Control.Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Window"> 
         <Grid Name="LayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="*" /> 
           <RowDefinition Height="Auto" /> 
          </Grid.RowDefinitions> 

          <Grid Grid.Row="0"> 
           <ContentPresenter /> 
          </Grid> 
          <StatusBar Name="statusBar1" Height="23" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="1" /> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Style> 
    <Grid> 
     <Grid Height="114" HorizontalAlignment="Center" Margin="1,0,557,622" Name="grid" VerticalAlignment="Bottom" Width="466"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto" /> 
       <ColumnDefinition Width="100*" /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="25*" /> 
       <RowDefinition Height="25*" /> 
       <RowDefinition Height="25*" /> 
       <RowDefinition Height="25*" /> 
      </Grid.RowDefinitions> 
      <Label Content="Label4" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Left" Margin="3,0,3,0" MinHeight="16" MinWidth="97" Name="xResourceLabel4" VerticalAlignment="Bottom" /> 
      <Label Content="Label1" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3,0,3,0" MinHeight="16" MinWidth="98" Name="xResourceLabel1" VerticalAlignment="Bottom" /> 
      <Label Content="Label2" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="3,0,3,0" MinHeight="16" MinWidth="114" Name="xResourceLabel2" VerticalAlignment="Bottom" /> 
      <Label Content="Label3" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Margin="3,0,3,0" MinHeight="16" MinWidth="80" Name="xResourceLabel3" VerticalAlignment="Bottom" /> 
      <TextBox Background="#FFFFFF99" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="120,0,189,0" MinHeight="23" MinWidth="157" Name="tbREGNR" VerticalAlignment="Bottom" /> 
      <TextBox Background="#FFFFFF99" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" Margin="0,0,189,0" MinHeight="23" MinWidth="157" Name="tbFaFabnr" VerticalAlignment="Bottom" /> 
      <ComboBox Background="#FFFFFF99" DisplayMemberPath="" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" Margin="0,0,189,0" MinHeight="24" MinWidth="157" Name="cb" SelectedValuePath="" VerticalAlignment="Bottom" /> 
      <TextBox Background="#FFFF99CC" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Center" Margin="0,0,175,0" MinHeight="23" MinWidth="171" Name="tbComp" VerticalAlignment="Bottom" /> 
     </Grid> 
    </Grid> 
</Window> 

回答

6
<Grid Name="LayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*" /> 
          <RowDefinition Height="Auto" /> 
         </Grid.RowDefinitions> 
         <Grid Grid.Row="0"> 
          <ContentPresenter /> 
         </Grid> 
         <StatusBar Name="statusBar1" Height="23" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="1" /> 
         </Grid> 

因爲你要重寫你搶走風格影響窗口的默認模板(其中一個是背景=黑根格)

見上面,我已經把它的模板,它現在應該工作(背景=「白」)

下面是一個窗口(取自混合)的默認模板

<ControlTemplate x:Key="WindowTemplateKey" TargetType="{x:Type Window}"> 
     <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> 
      <Grid> 
       <AdornerDecorator> 
        <ContentPresenter/> 
       </AdornerDecorator> 
       <ResizeGrip x:Name="WindowResizeGrip" HorizontalAlignment="Right" IsTabStop="false" Visibility="Collapsed" VerticalAlignment="Bottom"/> 
      </Grid> 
     </Border> 
     <ControlTemplate.Triggers> 
      <MultiTrigger> 
       <MultiTrigger.Conditions> 
        <Condition Property="ResizeMode" Value="CanResizeWithGrip"/> 
        <Condition Property="WindowState" Value="Normal"/> 
       </MultiTrigger.Conditions> 
       <Setter Property="Visibility" TargetName="WindowResizeGrip" Value="Visible"/> 
      </MultiTrigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
    <Style x:Key="WindowStyle1" TargetType="{x:Type Window}"> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> 
     <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Window}"> 
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> 
         <AdornerDecorator> 
          <ContentPresenter/> 
         </AdornerDecorator> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="ResizeMode" Value="CanResizeWithGrip"> 
       <Setter Property="Template" Value="{StaticResource WindowTemplateKey}"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
+0

解決了網格背景,但是如果我將窗口背景設置爲白色,它仍然是黑色! – Vale 2011-04-07 10:08:26

+0

這條線看起來很有趣''但是如何處理它,schould我重寫或什麼? – Vale 2011-04-07 10:10:54

+0

這是它設置你的窗口的背景,但因爲你的LayoutRoot網格設置爲伸展你不應該看到任何你的窗口的任何背景 – 2011-04-07 10:15:17