开发者

WPF: Relative Source to DataTemplate

开发者 https://www.devze.com 2023-01-26 15:06 出处:网络
If I have a data template as follows, <DataTemplate DataType=\"{x:Type vm:EditorTabViewModel}\">

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}" ... />
0

精彩评论

暂无评论...
验证码 换一张
取 消