2012-09-08 196 views
0

我有一個名爲EditorView的UserControl,根據其內容顯示不同的「編輯器」(其他用戶控件)。XAML綁定到ContentControl的UserControl的屬性

這是EditorView只是爲了測試結合我替換TextBlock中的字體編輯器:

<UserControl x:Class="TrikeEditor.UserInterface.EditorView" ...> 

    <UserControl.Resources> 
     <DataTemplate DataType="{x:Type te_texture:Texture}"> 
      <teuied:TextureEditor TextureName="{Binding Path=Name}"/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type te_font:Font}"> 
      <!--<teuied:FontEditor/>--> 
      <TextBlock Text="{Binding Path=Name}"/> 
     </DataTemplate> 
    </UserControl.Resources> 

    <UserControl.Template> 
     <ControlTemplate TargetType="{x:Type UserControl}"> 
      <ContentPresenter Content="{TemplateBinding Content}" x:Name="EditorPresenter"/> 
     </ControlTemplate> 
    </UserControl.Template> 


</UserControl> 

合適的模板是基於EditorView.Content和TextBlock的情況下,結合作品越來越選爲期望,但在TextureEditor的情況下,TextureName屬性不是。

下面是從TextureEditor片段:

public partial class TextureEditor : UserControl 
{ 
    public static readonly DependencyProperty TextureNameProperty = DependencyProperty.Register("TextureName", typeof(string), typeof(TextureEditor)); 
    public string TextureName 
    { 
     get { return (string)GetValue(TextureNameProperty); } 
     set { SetValue(TextureNameProperty, value); } 
    } 

    public TextureEditor() 
    { 
     InitializeComponent(); 
    } 
} 

有什麼特別的,我必須做的,因爲我使用的用戶控件?也許是不同的命名空間是問題?

回答

1

用戶控件不應該影響它;不同之處在於您正在實現自己的依賴項屬性(而不是使用文本中的TextBlock)。你必須設置的依賴項屬性TextureName屬性值的PropertyChanged處理程序:

public static readonly DependencyProperty TextureNameProperty = 
    DependencyProperty.Register("TextureName", typeof(string), typeof(TextureEditor), 

    // on property changed delegate: (DependencyObject, DependencyPropertyChangedEventArgs) 
    new PropertyMetadata((obj, args) => { 

     // update the target property using the new value 
     (obj as TextureEditor).TextureName = args.NewValue as string; 
    }) 
);