2014-01-24 67 views
1

我使用Visual Studio 2013創建我的應用程序。 4年前我開始學習編程,現在我想改善我的程序員經驗。窗口風格和使用控件(C#和WPF)

在最後幾天我不得不創建一個簡單的應用程序,但我不想使用窗口的默認樣式。所以我讀到我可以爲我的窗口創建一種風格。我做到了,它工作正常,但有一個小問題:我現在不能在窗口中放入任何東西......我將所有控件放入網格中,放入窗口標記中,都不可見,但編譯器不會顯示沒有任何問題。

這裏是樣式代碼,然後我的窗口XAML

<Style TargetType="Window" x:Key="DefaultWindow"> 
     <Setter Property="AllowsTransparency" Value="True"/> 
     <Setter Property="WindowStyle" Value="None"/> 
     <Setter Property="ResizeMode" Value="CanResizeWithGrip"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Window}">      
        <Border BorderThickness="1" BorderBrush="Black"> 
         <Border BorderThickness="3" BorderBrush="#FF244E97"> 
          <Grid> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="50"></RowDefinition> 
            <RowDefinition></RowDefinition> 
           </Grid.RowDefinitions> 

           <Grid Grid.Row="0" Background="#FF244E97"> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition></ColumnDefinition> 
             <ColumnDefinition Width="Auto"></ColumnDefinition> 
            </Grid.ColumnDefinitions> 

            <Viewbox Grid.Column="0" Grid.Row="0" Stretch="Uniform" HorizontalAlignment="Left" Margin="10"> 
             <TextBlock FontFamily="Corbel" Background="#FF244E97" Foreground="White">Costo Unitario</TextBlock> 
            </Viewbox> 

            <Grid Grid.Column="1"> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition></ColumnDefinition> 
              <ColumnDefinition></ColumnDefinition> 
              <ColumnDefinition></ColumnDefinition> 
             </Grid.ColumnDefinitions> 

             <Viewbox Grid.Column="0" Stretch="Uniform" HorizontalAlignment="Left" Margin="2, 10, 2, 10"> 
              <TextBlock FontFamily="Corbel" Background="#FF244E97" Foreground="White">_</TextBlock> 
             </Viewbox> 

             <Viewbox Grid.Column="1" Stretch="Uniform" HorizontalAlignment="Left" Margin="2, 10, 2, 10"> 
              <TextBlock FontFamily="Corbel" Background="#FF244E97" Foreground="White">◘</TextBlock> 
             </Viewbox> 

             <Viewbox Grid.Column="2" Stretch="Uniform" HorizontalAlignment="Left" Margin="2, 10, 20, 10"> 
              <TextBlock FontFamily="Corbel" Background="#FF244E97" Foreground="White">X</TextBlock> 
             </Viewbox> 

            </Grid> 

           </Grid> 

           <Grid x:Name="ContenentGrid" Grid.Row="1" Background="White"> 
            <ResizeGrip Width="10" Height="10" Grid.Column="1" VerticalAlignment="Bottom"/> 
           </Grid> 

          </Grid>                
         </Border> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

而且我的Windows的XAML:

<Window x:Class="Pabich.Marcin._5HI.CostoUnitario.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:chrt="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
    Title="MainWindow" Height="600" Width="800" WindowStartupLocation="CenterScreen" 
    Style="{StaticResource DefaultWindow}"> 

<Grid> 
    <TextBlock>Hello!</TextBlock> 
</Grid> 

這應該是一個無邊界的窗口,內我可以把控制,在中央電網。這是現在窗口的一個屏幕: Screen of the Window

正如你所看到的,我將所有的空白空間都留給了我的控件,但它們不在那裏。那我該怎麼做呢?

PS:請記住,我只能是一個初學者,我想用簡單的代碼

+0

您的模板正在設置邊框,並且您在該模板中指定的所有內容都是邊框的一部分,包括帶有x:Name =「ContentGrid」的網格。我認爲你的邊框可能正在填滿屏幕,並且內容(你好!)低於它 - 並且在可見區域的底部。將您的ContentGrid背景設置爲紅色並查看它的外觀。 –

回答

2

我認爲這是因爲你不使用ContentPresenter顯示窗口內容。沒有ContentPresenter WPF內容模型不知道應該放置內容的位置,並且不會顯示它。

嘗試將ContentPresenter添加到您的ContentGrid(WPF窗口也要求在AdornerDecorator中)。

<Grid x:Name="ContenentGrid" Grid.Row="1" Background="White"> 
    <AdornerDecorator> 
     <ContentPresenter Content="{TemplateBinding Content}" /> 
    </AdornerDecorator> 
    ... ResizeGrip here ... 
</Grid> 
+0

是的,它的工作原理。這是我的問題的解決方案。謝謝! – Martinocom

+0

不客氣!我很高興我的回答對你有幫助。請將我的答案標記爲其他人可以看到的正確答案。 – neptunao