2012-03-25 79 views
3

我有一個窗口,它是這樣如何創建其他窗口繼承的窗口?

<Window x:Class="pharmacy_Concept.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 

     <Button Content="Login" Height="34" HorizontalAlignment="Left" Margin="12,241,0,0" Name="loginbutton" VerticalAlignment="Top" Width="129" Click="loginbutton_Click" /> 
     <Button Content="Exit" Height="34" HorizontalAlignment="Left" Margin="362,241,0,0" Name="Exitbutton" VerticalAlignment="Top" Width="129" Click="Exitbutton_Click" /> 
    </Grid> 
</Window> 

我想我已經創建了這個layout.Do我必須使用一個資源字典this.If所以如何每一個新的窗口?還是我要做別的事

這只是爲了把握這個概念。我將在以後使用圖像和標籤。

回答

3

你應該聲明一個ControlTemplate,你通常在ResourceDictionary中定義。例如:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > 

<Style x:Key="{x:Type Window}" TargetType="{x:Type Window}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Window}"> 
       <Grid Background="Red"> 
        <Button Content="Login" Height="34" HorizontalAlignment="Left" Margin="12,241,0,0" Name="loginbutton" VerticalAlignment="Top" Width="129" Click="loginbutton_Click" /> 
        <Button Content="Exit" Height="34" HorizontalAlignment="Left" Margin="362,241,0,0" Name="Exitbutton" VerticalAlignment="Top" Width="129" Click="Exitbutton_Click" /> 
       </Grid> 

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

那麼你應該在app.xaml添加此應用程序資源:

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Window.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

而在你的窗口中使用了這樣的:

Style="{StaticResource {x:Type Window}}" 
+0

它說我應該添加一個X:class屬性 – deception1 2012-03-25 06:47:26

+0

確實應該添加另一個類來處理事件在您的資源文件 – ionden 2012-03-25 06:49:40

+0

和Style =「{StaticResource {x:Type Window}}」 也表示必須具有非null值setter.property – deception1 2012-03-25 06:53:20