C
C#2y ago
twiner

❔ Custom DataTemplate for TextElements or alternative approaches

In WPF I'm working on a customized RichTextBox that will add TextElements that are bound to view models. I want to DataTemplates for this, but you can't put TextElements in a DataTemplate. I've been looking into DataTemplate and FrameworkTemplate to see how they work, but its quite involved and maybe I'm over thinking this. I want to be able to supply, in xaml preferably, a collection of mappings between a partially configure DataType and a template that will be bound to it. This is the code I have for the association at the moment, making a copy of the DataContext is easy enough, but I don't know a what to do to make a copy of the ElementTemplate and maintain the binding information.
<x:Array x:Key="DetailTypes" Type="{x:Type v:ContextOption}">
<v:ContextOption DisplayName="Header">
<v:ContextOption.DataContext>
<m:AddDescriptionDetail Style="Header" />
</v:ContextOption.DataContext>
<v:ContextOption.ElementTemplate>
<rt:Header Text="{Binding Path=Description}"/>
</v:ContextOption.ElementTemplate>
</v:ContextOption>
</x:Array>
<x:Array x:Key="DetailTypes" Type="{x:Type v:ContextOption}">
<v:ContextOption DisplayName="Header">
<v:ContextOption.DataContext>
<m:AddDescriptionDetail Style="Header" />
</v:ContextOption.DataContext>
<v:ContextOption.ElementTemplate>
<rt:Header Text="{Binding Path=Description}"/>
</v:ContextOption.ElementTemplate>
</v:ContextOption>
</x:Array>
[ContentProperty(nameof(ElementTemplate))]
public class ContextOption
{
public IShallowCloner DataContext { get; set; }
public string DisplayName { get; set; }
public TextElement ElementTemplate { get; set; }
}
[ContentProperty(nameof(ElementTemplate))]
public class ContextOption
{
public IShallowCloner DataContext { get; set; }
public string DisplayName { get; set; }
public TextElement ElementTemplate { get; set; }
}
2 Replies
twiner
twinerOP2y ago
My aim is to make it so I can easily add new type associations and how they will be rendered in the FlowDocument Seems this is doable to a limited extent by implementing ICloneable and using MemberwiseClone This limits its usage to custom types
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server