2

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 EditorTabViewModel.Content ^ refers to EditorTabViewModel 

Is there a more ituitive markup? lol, i'd thought RelativeSource={RelativeSource Mode=TemplatedParent}}" already refers to EditorTabViewModel at first

1 Answer 1

1

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}" ... /> 
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm but if my MarkdownEditor is a UserControl that has DataContext = this I need to do that or else it will try to bind to a property of MarkdownEditor right?
Inside any DataTemplate the DataContext is always the data object the template is being applied to. When you have an explicitly specified DataType as in your example the DataContext will be an instance of that type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.