2012-09-08 52 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; 
    }) 
); 
相关问题