Such situation. There is a class
public partial class HighlightTextBlock : TextBlock { public ObservableCollection<HighlightRule> HighlightRules { get { return (ObservableCollection<HighlightRule>)GetValue(HighlightRulesProperty); } set { SetValue(HighlightRulesProperty, value); } } // Using a DependencyProperty as the backing store for HighlightRules. This enables animation, styling, binding, etc... public static readonly DependencyProperty HighlightRulesProperty = DependencyProperty.Register("HighlightRules", typeof(ObservableCollection<HighlightRule>), typeof(HighlightTextBlock), new FrameworkPropertyMetadata(new ObservableCollection<HighlightRule>())); // Ну далее еще много чего тут } But the HighlightRule class
public class HighlightRule : DependencyObject { public ObservableCollection<Highlight> Highlights { get { return (ObservableCollection<Highlight>)GetValue(HighlightsProperty); } set { SetValue(HighlightsProperty, value); } } // Using a DependencyProperty as the backing store for Highlights. This enables animation, styling, binding, etc... public static readonly DependencyProperty HighlightsProperty = DependencyProperty.Register("Highlights", typeof(ObservableCollection<Highlight>), typeof(HighlightRule), new PropertyMetadata(new ObservableCollection<Highlight>())); // Тут тоже много чего еще } This is what XAML usage looks like.
<ListBox ItemsSource="{Binding Strings}" Grid.Row="1"> <ListBox.ItemTemplate> <DataTemplate> <local:HighlightTextBlock Text="{Binding}"> <local:HighlightTextBlock.HighlightRules> <local:HighlightRule HightlightedText="{Binding Filter, Source={x:Reference thisWindow}}"> <local:HighlightRule.Highlights> <local:HighlightBackgroung Brush="Yellow"/> </local:HighlightRule.Highlights> </local:HighlightRule> </local:HighlightTextBlock.HighlightRules> </local:HighlightTextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> Well, everything seems to be normal, but it was too slow, so I got into debugging and saw that there are exactly as many highlights for each rule as there are rules. That is, in this case, the number of elements of the collection. Maybe I do not correctly use the properties of dependencies, because each rule refers to the same highlights (although, as can be seen from xamla, each rule corresponds to a single backlight in this case).
UPD:
Here is a project like VladD