Deferred "rendering" WPF / Silverlight dependency properties?

Is there a way to know when a dependency resource is first accessed through XAML binding so I can actually "render" the property value when needed?

I have an object (a class derived from Control) that has several PointCollection dependency properties that can hold 100 or 1000 points. Each property can position the points differently for use in different types of shapes (Polyline, Polygon, etc.), which is more complicated than that, but you get the idea). With a template, various XAML objects use the TemplateBinding to access these properties. Since my object uses a template, I never know what XAML forms can be used for my object, so I never know what properties they might or might not bind to. I would only like to populate these PointCollections when I really need them.

Normally in .NET I would use some logic in the getter of the property, but these are bypassed by XAML data binding.

I need a WPF and Silverlight solution.

I would like a solution that avoids any additional complications for the users of my facility.


Update

One of the ways I've found to do this is using Value Converters . In my situation, I had multiple point collections. There was the main department. property containing the usual form of data. For reuse in other areas / contexts, two alternative forms are needed.

At first I had 3 dep. props. But I could only have one property (normal form) and used the converted value to convert the points to my other 2 desired shapes. By doing this, I am only making one set of dots per control. The consumption of transformation points for secondary figures occurs only during use. Now my main control doesn't have to anticipate how the data should look for all possible patterns thrown into the control - it's now a problem for the template designers.


Update 2

Of course, INotifyPropertyChanged and regular properties are the recommended way to deal with this.

+2


source to share


2 answers


It is not necessary to use dependency properties to enable data binding. However, then you need to implement INotifyPropertyChanged

if the changes in the source are to propagate to the binding target. A "regular" .NET property is easy to lazy, perhaps like this:

PointCollection points

public PointCollection Points {
  get {
    return this.points ?? (this.points = CreatePoints());
  }
}

PointCollection CreatePoints() {
  // ...
}

      



I'm not sure how you can put it INotifyPropertyChanged

in your control, but it sounds a bit odd that your control is passing data to other parts of the system. Perhaps you need to create a view model that contains data, which you can then pass to the control's data binding.

+3


source


If I rephrase your question to

How do I get notified when a dependency property changes?

It will be right? I draw this from your phrase "Normally in .NET I would have some kind of logic in the Property getter, but they get around the XAML data binding."



If I am correct then you can register your own modified callback property. It has always been called. It doesn't matter who caused the binding change, style, or trigger. The following code snippet is from the MSDN article " Dependency Property Callbacks and Validation" :

public static readonly DependencyProperty CurrentReadingProperty = 

    DependencyProperty.Register(
        "CurrentReading",
        typeof(double),
        typeof(Gauge),
        new FrameworkPropertyMetadata(
            Double.NaN,
            FrameworkPropertyMetadataOptions.AffectsMeasure,
            new PropertyChangedCallback(OnCurrentReadingChanged),
            new CoerceValueCallback(CoerceCurrentReading)
        ),
        new ValidateValueCallback(IsValidReading)
    );
    public double CurrentReading
    {
      get { return (double)GetValue(CurrentReadingProperty); }
      set { SetValue(CurrentReadingProperty, value); }
    }

      

Your contribution here is the OnCurrentReadingChanged () method. Hope this helps :).

0


source







All Articles