2011-05-05 59 views
2

我在generic.xaml中創建了一個樣式,我想在我的項目中使用幾個UserControls。以同樣的方式我已經定義了一個風格的自定義控制和這一個工程這麼看來generic.xaml被加載,這是定義的樣式:Howto:將generic.xaml中定義的樣式應用於UserControl? (WPF)

<Style TargetType="{x:Type UserControl}" x:Key="ServiceStyle" x:Name="ServiceStyle"> 
    <Setter Property="Opacity" Value="0.5"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type UserControl}"> 
       <Border Name="border" CornerRadius="20" 
         Margin="10" 
         BorderThickness="5" 
         BorderBrush="Black"> 
         <ContentPresenter Content="{TemplateBinding Content}" 
              Margin="{TemplateBinding Padding}"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

但現在我想用這種風格,但我不能讓它工作。我已經tryed將其添加爲一個風格參數用戶控件的一個自定義實例,通過以下方式:

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Netcarity" 
    xmlns:CustomControls="clr-namespace:Netcarity.CustomControls" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Portier_deur" x:Class="Netcarity.UserControlPortier" 
    Height="600" Width="800" MouseDown="UserControl_MouseDown" Loaded="UserControl_Loaded" mc:Ignorable="d" 
    Style="{StaticResource ServiceStyle}"> 

但是這給了我一張紙條,資源ServiceStyle找不到。試圖運行時,會出現運行時錯誤。

在此先感謝。

+0

重新標記,假設你想要'WPF'而不是'WFP' – 2011-05-16 12:29:08

回答

0

Generic.xaml似乎不適合存儲非自定義控件的樣式。在某處我發現了將syle放入App.xaml而不是generic.xaml的提示,並且直接運行。因此它接縫Generic.xaml只能用於存儲customControls的樣式。 也許有人可以爲此行爲添加更多受過教育的理由?

0

當Generic.xaml使用ComponentResourceKey。