2012-11-16 19 views
0

我想要定義一個控制模板,然後我想用於模態對話框。問題是,我遵循了我可以在stackoverflow和其他任何地方找到的所有說明,但樣式/模板未加載並應用?相反,我得到一個靜態資源異常。如何將控制模板分配給Wpf中的窗口?

那麼,如果模板和窗口在不同的文件中,如何將模板應用到我的窗口?

任何幫助?

<Window x:Class="WpfWindowTemplateTest.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" 
     Template="{StaticResource MyWindowTemplate}" 
     Style="{StaticResource MyWindowStyle}" /> 

的模板,我用是這樣的一個:

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

    <ControlTemplate x:Key="MyWindowTemplate" TargetType="{x:Type Window}"> 
     <Border x:Name="WindowBorder" Style="{DynamicResource WindowBorderStyle}"> 
      <Grid> 
       <Border Margin="4,4,4,4" Padding="0,0,0,0" x:Name="MarginBorder"> 
        <AdornerDecorator> 
         <ContentPresenter/> 
        </AdornerDecorator> 
       </Border> 
       <ResizeGrip Visibility="Collapsed" IsTabStop="false" HorizontalAlignment="Right" x:Name="WindowResizeGrip" 
        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"/> 
       <Setter Property="Margin" TargetName="MarginBorder" Value="4,4,4,20" /> 
      </MultiTrigger> 
      <Trigger Property="WindowState" Value="Maximized"> 
       <Setter Property="CornerRadius" TargetName="WindowBorder" Value="0,0,0,0"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
    <Style x:Key="MyWindowStyle" TargetType="{x:Type Window}"> 
     <Setter Property="AllowsTransparency" Value="False" /> 
     <Setter Property="WindowStyle" Value="SingleBorderWindow" /> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> 
     <Setter Property="Background" Value="Transparent" /> 
     <Setter Property="ShowInTaskbar" Value="False" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Window}"> 
        <Border> 
         <AdornerDecorator> 
          <ContentPresenter/> 
         </AdornerDecorator> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style>  
</ResourceDictionary> 
+0

什麼是確切的錯誤,而這哪裏是ResourceDictionary中加載? –

回答

2

使用DynamicResource替代StaticResource的。

<Window x:Class="WpfWindowTemplateTest.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" 
    Template="{DynamicResource MyWindowTemplate}" 
    Style="{DynamicResource MyWindowStyle}" /> 

添加到您的App.xaml

<ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="/WpfWindowTemplateTest;component/MyWindowTemplate.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 
+0

也不起作用。然後窗口保持黑色。這就是爲什麼我現在爲此瘋狂了幾個小時。 – Matthias

+0

奇怪的是,我已經能夠以這種方式設置一個ControlTemplate到我的主窗口:/ – Sisyphe

+0

當然我會試一試 – Sisyphe

相關問題