2013-07-26 73 views
0

我創造了這個控制WPF自定義擴展的控制並沒有呈現

[ContentProperty("Text")] 
[DefaultProperty("Text")] 
public class TextElement : Control { 

    public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", typeof(string), 
     typeof(TextElement), new PropertyMetadata(default(string))); 

    public static readonly DependencyProperty LabelProperty = 
     DependencyProperty.Register("Label", typeof(string), 
     typeof(TextElement), new PropertyMetadata(default(string))); 

    [Bindable(true)] 
    public string Text { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    [Bindable(true)] 
    public string Label { 
     get { return (string)GetValue(LabelProperty); } 
     set { SetValue(LabelProperty, value); } 
    } 
} 

另外,我在項目定義的控件的默認模板中/Themes/Generic.xaml

<Style x:Key="{x:Type local:TextElement}" TargetType="{x:Type local:TextElement}"> 
    <Setter Property="SnapsToDevicePixels" Value="True"/> 
    <Setter Property="HorizontalAlignment" Value="Stretch" /> 
    <Setter Property="HorizontalContentAlignment" Value="Left"/> 
    <Setter Property="VerticalAlignment" Value="Stretch" /> 
    <Setter Property="VerticalContentAlignment" Value="Top" /> 
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrush}}"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrush}}"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:TextElement}"> 
       <Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
         VerticalAlignment="{TemplateBinding VerticalAlignment}" 
         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
         Background="Yellow" 
         Width="100" Height="30"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="55"/> 
         <ColumnDefinition Width="66"/> 
        </Grid.ColumnDefinitions> 
        <Label Grid.Column="0" x:Name="label" Background="Red" Height="20" Width="20" 
          HorizontalAlignment="Stretch" 
          HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
          VerticalAlignment="Stretch" 
          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
          Content="{TemplateBinding Label}" 
          Style="{TemplateBinding LabelStyle}"/> 
        <ContentPresenter /> 
        <TextBlock Text="{TemplateBinding Text}"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

但是,當我使用它在一個視圖中,沒有顯示。我錯在哪裏?請任何想法嗎?

回答

1

你必須重寫DefaultStyleKey元數據像下面,以表明您已經於Generic.xaml

DefaultStyleKeyProperty.OverrideMetadata(typeof(TestItemsControl), new FrameworkPropertyMetadata(typeof(TestItemsControl))); 

定義的樣式加入控制

+0

謝謝,我也發現了這個問題:D。但是要說謝謝,我會接受並投票回答你的答案。乾杯 –

0

哎呀的靜態構造函數裏面的代碼!我發現了這個問題。我忘了重寫元數據。如果遇到同樣的問題,就把它放在這裏幫助別人。

// in static .ctor 
static TextElement() { 
    DefaultStyleKeyProperty.OverrideMetadata(
     typeof (TextElement), new FrameworkPropertyMetadata(typeof (TextElement))); 
} 
相關問題