WPF: Relative Source to DataTemplate
If I have a data template as follows,
<DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
<me:MarkdownEditor
TextContent="{Binding
Path=Content,
RelativeSource={RelativeSource AncestorType={x:Type vm:EditorTabViewModel}}}" ... />
</DataTemplate>
I wonder why Relative source can't find the EditorTabViewModel
? Its just referring to the data template?
UPDATE 1
The below works tho
<me:MarkdownEditor TextContent="{Binding Path=Content.Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
Binding Path=Content.Content
^ Refers to EditorTabV开发者_运维技巧iewModel.Content
^ refers to EditorTabViewModel
Is there a more ituitive markup? lol, i'd thought RelativeSource={RelativeSource Mode=TemplatedParent}}"
already refers to EditorTabViewModel
at first
Given that the DataTemplate
is for objects of type EditorTabViewModel
, its DataContext
will already be a EditorTabViewModel
. So you don't need to set a source at all:
TextContent="{Binding Content}" ... />
精彩评论