Delayed “rendering” of WPF/Silverlight Dependency Properties?

佐手、 提交于 2019-12-11 02:18:35

问题


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

I have an object (class derived from Control) that has several PointCollection Dependency Properties that may contain 100's or 1000's of points. Each property may arrange the points differently for use in different types shapes (Polyline, Polygon, etc - its more complicated then this, but you get the idea). Via a Template different XAML objects use TemplateBinding to access these properties. Since my object uses a Template I never know what XAML shapes may be in use for my object - so I never know what Properties they may or may not bind to. I'd like to only fill-in these PointCollections when they are actually needed.

Normally in .NET I'd but some logic in the Property's getter, but these are bypassed by XAML data binding.

I need a WPF AND Silverlight compatible solution.

I'd love a solution that avoids any additional complexities for the users of my object.


Update

One way that I've found to do this is using Value Converters. In my situation I had multiple point collections. There was a main dep. property that contained the usual shape of the data. Two alternate shapes were needed for reuse in other areas/contexts.

At first I had 3 dep. props. But, I could have just had one property (the usual shape) and used a value converted to transform the points into my other 2 desired shapes. Doing this I only make the one set of points in the control. The expense of transforming points to the secondary shapes is only incurred when used. Now my main control doesn't need to anticipate how data needs to look for every possible template thrown at the control - now its the template designers problem.


Update 2

Certainly INotifyPropertyChanged and regular properties are the recommended way to handle this.


回答1:


You don't necessarily have to use dependency properties to enable data-binding. However, you then have to implement INotifyPropertyChanged if changes at the source should be propagated to the target of the binding. A "normal" .NET property is easy to lazy load perhaps like this:

PointCollection points

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

PointCollection CreatePoints() {
  // ...
}

I'm not sure how you can fit INotifyPropertyChanged into your control, but it sounds a bit strange that your control supplies data to other parts of the system. Perhaps you need to create a view-model containing the data that you then can let your control data-bind to.




回答2:


If I paraphrase your question to

How do I get notified when dependency property is changed?

will this be correct? I draw this from your phrase "Normally in .NET I'd but some logic in the Property's getter, but these are bypassed by XAML data binding".

If I'm correct, then you can register your own property changed callback. It's always called. Doesn't matter who caused the change binding, style or trigger. The following code snippet is taken from 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 takeaway here is OnCurrentReadingChanged() method. Hope this helps :).



来源:https://stackoverflow.com/questions/1465885/delayed-rendering-of-wpf-silverlight-dependency-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!