SilverLight 4的Microsoft Live Labs PivotViewer控件的當前版本無法對控件的元素進行樣式設置。看看Reflector中的控件,我可以看到很多樣式信息在程序集中的ResourceDictionary中設置(assets/defaultcolors.xaml)。我想要做的是創建我自己的這個文件的副本,然後在運行時在PivotViewer控件上替換它。如何替換第三方UserControl的ResourceDictionary(在本例中爲PivotViewer)
通過繼承PivotViewer控件並覆蓋OnApplyTemplate,我可以獲取子元素並設置背景等屬性。我還沒有成功清除()'NG的MergedDictionaries並加入我自己的:
public override void OnApplyTemplate() {
base.OnApplyTemplate();
/* can change things this way */
CollectionViewerView cvv = ((CollectionViewerView)((Grid)this.GetTemplateChild("PART_Container")).Children[0]);
((Grid)cvv.Content).Background = new SolidColorBrush(Colors.Black);
/* can't change things this way */
CustomDictionary gd = new CustomDictionary();
cvv.Resources.MergedDictionaries.Clear();
cvv.Resources.MergedDictionaries.Add(gd);
}
謝謝 - 標記爲答案。有時間重新考慮這種方法。對於PivotViewer來說,視覺樹是非常討厭的,試圖設計它的樣式,那會是瘋狂的! – ViNull 2010-09-16 13:22:03