2010-09-16 75 views
1

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); 

}

回答

0

這恐怕是不會在Silverlight中工作,因爲它僅使用靜態資源。 (Styles Don't Update

只有在調用InitializeComponent()之前,更改資源字典才能在PivotViewer的構造函數中調用它。

我一直在嘗試設計PivotViewer控件的樣式。我希望有另外一種方式,除了通過可視化樹和改變屬性。

+0

謝謝 - 標記爲答案。有時間重新考慮這種方法。對於PivotViewer來說,視覺樹是非常討厭的,試圖設計它的樣式,那會是瘋狂的! – ViNull 2010-09-16 13:22:03

相關問題