我有一个依赖对象,它定义了一个名为“Renderer”的依赖属性。实现DependencyObject和INotifyPropertyChanged
public class Renderer {
public string ResourceKey{get; set;}
public string[] Params{get; set;}
}
public class CellInfo : DependencyObject {
public static readonly DependencyProperty RendererProperty=
DependencyProperty.Register("Renderer", typeof(Renderer), typeof(CellInfo), null);
public Renderer Renderer {
get { return (Renderer)GetValue(RendererProperty); }
set { SetValue(RendererProperty, value); }
}
public void UpdateRenderer(string resourceKey, params string[] parameters) {
this.Renderer.ResourceKey = resourceKey;
this.Renderer.Params = parameters;
//force refresh - this does not work
Renderer tmp = this.Renderer;
this.Renderer = null;
this.Renderer = tmp;
}
}
在XAML我声明了这样的:
<local: CellInfo x:key="cellInfo" />
,并进一步向下我结合它是这样:
<ControlTemplate x:Key="MyDisplayTemplate" >
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Stretch" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource MyConverter}" >
<Binding Path="Value" />
<Binding Source="{StaticResource cellInfo}" Path="Renderer"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ControlTemplate>
上面对照模板被用作显示网格单元格的模板。我想在性能方面使用依赖对象/属性,因为据我所知,它们在执行INotifyPropertyChanged
时确实具有性能和内存资源优势。
当Renderer对象的数据发生变化时,我遇到的问题是触发单元格的更新。在代码中,我试图解决这个事实,即如果值是相同的,则依赖属性不会触发更新,通过首先设置为空,然后设置回原始属性值。这不起作用。
我已经能够上班的唯一的事情是也有对象执行INotifyPropertyChanged
和解雇PropertyChanged
事件CellInfo.Renderer
二传手。
我的猜测是必须实施INotifyPropertyChange
否定仅使用DependencyObjects
的性能好处,是否正确?此时,如果我被迫执行INotifyPropertyChanged
,我甚至可能不会延长DependencyObject
,对吗?
感谢您的任何意见。
当'class Renderer'变得不可变时,这会更容易。 – 2012-07-19 17:11:28
为什么?并且我的假设是正确的(请参见最后一段) – mike01010 2012-07-19 17:13:58
不可变的值必须一次全部换出并触发通知。 – 2012-07-19 17:19:11