2013-11-21 67 views
0

我無法將樣式應用於自定義用戶控件。 樣式爲TextBlockMyControl位於同一字典中。 風格爲TextBlock使用沒有問題。 但對於MyControl樣式未應用。 爲什麼?我無法將樣式應用於自定義用戶控件

// XAML 
xmlns:local="clr-namespace:MyControlNameSpace" 

<Grid> 
    <TextBlock x:Name="text" Style="{DynamicResource TextStyle}"/> 
    <local:MyControl x:Name="control" Style="{DynamicResource ControlStyle}"/> 
</Grid> 

// RESOURCE DICTIONARY 
    <Style x:Key="ControlStyle" TargetType="{x:Type local:MyControl}"> 
     <Setter Property="Height" Value="Auto"/> 
     <Setter Property="Width" Value="Auto"/> 
     <Setter Property="Foreground" Value="White"/> 
     <Setter Property="ColorMain" Value="Black"/> 
     <Setter Property="Margin" Value="2"/> 
    </Style> 

    <Style x:Key="TextStyle" TargetType="{x:Type TextBlock}"> 
     .... 
     <Setter Property="FontSize" Value="32"/> 
     ... 
    </Style> 
 //PART of CS   
    public partial class MyControl: UserControl { .... } 
+0

這一部分看起來不錯,張貼MyControl的XAML。 –

+0

你肯定使用MyControl的具體實例?不是它的子類? – GazTheDestroyer

+0

除非您打算在運行時對'Style'進行更改,否則應該使用'StaticResource'而不是'DynamicResource'](http://stackoverflow.com/questions/200839/whats-the-difference-間的StaticResource和 - dynamicresource合WPF)。 – Sheridan

回答

0

試着將

<Setter Property="ColorMain" Value="Black"/> 

<Setter Property="Background" Value="Black"/> 
相關問題