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

download

    1 answer 1

    The problem is where:

     new PropertyMetadata(new ObservableCollection<Highlight>()) 

    You get that you make the same ObservableCollection<Highlight> instance the default Highlights property for all HighlightRule instances! This problem is usually invisible if your property is a value type, since it is cloned when copying, unlike the reference type.

    Do this:

     public class HighlightRule : DependencyObject { public HighlightRule() { Highlights = new ObservableCollection<Highlight>(); } public ObservableCollection<Highlight> Highlights { get { return (ObservableCollection<Highlight>)GetValue(HighlightsProperty); } set { SetValue(HighlightsProperty, value); } } public static readonly DependencyProperty HighlightsProperty = DependencyProperty.Register( "Highlights", typeof(ObservableCollection<Highlight>), typeof(HighlightRule)); // Тут тоже много чего еще } 

    The same goes for HighlightTextBlock , of course.

    • I guessed that this was exactly the problem and I tried to do so, as you suggested, but in this case all my content in the xaml code went nowhere. I do not know why. The idea is to call the constructor, create a property, then fill it with xaml. but in practice all collections in this case are null. - iRumba
    • @iRumba: Strange. We will think. - VladD
    • @iRumba: Strange, it works for me. Can you concoct a reproducing example? - VladD
    • Download link in question. Visual Studio 2015 - iRumba
    • And though .... it seems like all the rules ... I’ve seen it yesterday already. The problem is different now, but I'll figure it out myself :) - iRumba