原創|使用教程|編輯:龔雪|2022-06-24 10:10:40.137|閱讀 205 次
概述:本文主要為大家介紹如何使用Telerik WPF控件在運行時使用主題切換自定義樣式,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
使用隱式樣式設置主題,您可以選擇在運行時更改控件的主題,而無需重新創建 UI。
合并字典中的資源在資源鎖定范圍中占據一個位置,該位置正好在它們合并到的主資源字典的范圍之后。 您可以做的是將自定義樣式隔離在單獨的資源字典中,并在每次更改主題時將它們添加到默認字典之后。
例如,您可以按照以下步驟操作:
1. 創建一個新應用程序并從位于Telerik UI for WPF安裝文件夾中的 Binaries.NoXaml 文件夾中添加所需的程序集以及主題程序集:
2. 在 App.xaml 中為默認主題添加相應的資源字典:
示例 1:合并資源字典
XAML
<ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml"/> <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml"/> <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Input.xaml"/> <!-- ... --> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
注意:最初,我們合并 Office_Black 主題的資源字典。
3. 將您選擇的一些控件添加到應用程序的布局根目錄中,還有兩個用于在主題之間切換的按鈕。
示例 2:添加按鈕來在主題之間切換
XAML
<Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <telerik:RadButton Content="Button" VerticalAlignment="Center" Width="100"/> <StackPanel Grid.Row="1" Orientation="Horizontal"> <Button x:Name="Office_Black" Margin="5" Content="Office__Black" Click="Office_Black_Click"/> <Button x:Name="Office2016" Margin="5" Content="Office2016" Click="Office2016_Click"/> </StackPanel> </Grid>
4. 現在,將自定義樣式添加到項目的 Themes 文件夾中名為 CustomStyles_Office_Black.xaml 和 CustomStyles_Office2016.xaml的不同主題的單獨資源字典中,這些自定義資源字典將具有以下內容:
示例 3:在單獨的資源字典中添加自定義樣式
XAML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="telerik:RadButton" BasedOn="{StaticResource RadButtonStyle}"> <Setter Property="Background" Value="Black"/> <Setter Property="Foreground" Value="White"/> </Style> </ResourceDictionary>
注意:為不同的主題創建單獨的資源字典允許在每個主題的基礎上輕松定制,但是您可以在切換主題時使用單個字典并僅合并此字典,前提是您沒有任何特定于主題的更改。此類更改將包括任何修改過得空間模板,因為這些模板因主題而異,并且可能在切換時導致錯誤。
示例 4:將字典添加到 MergedDictionaries
XAML
<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml"/> <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml"/> <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Input.xaml"/> <!-- ... --> <ResourceDictionary Source="/ProjectName;component/Themes/CustomStyles_Office_Black.xaml"/> </ResourceDictionary.MergedDictionaries>
請注意,您應該將 ProjectName 替換為項目的實際名稱。
6. 然后,在按鈕的 Click 處理程序中,我們將從應用程序資源中清除合并的字典,并將主題程序集中的新資源字典與自定義資源字典中包含的自定義樣式合并:
示例 5:單擊按鈕時清除和合并字典
C#
private void Office_Black_Click(object sender, RoutedEventArgs e) { this.MergeDictionaries("Office_Black"); } private void Office2016_Click(object sender, RoutedEventArgs e) { this.MergeDictionaries("Office2016"); } private void MergeDictionaries(string theme) { Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes." + theme + ";component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) }); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes." + theme + ";component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) }); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes." + theme + ";component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) }); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/ProjectName;component/Themes/CustomStyles_" + theme + ".xaml", UriKind.RelativeOrAbsolute) }); }
請注意,MergeDictionaries 中提供的主題應與相應主題程序集的名稱匹配,例如 - Expression_Dark、Office2016Touch、Material。 使用這種方法,您可以切換到Telerik UI for WPF套件提供的任何主題。
基于上述代碼的結果如圖 1 所示。
圖 1:帶有 Office_Black 和 Office 2016 主題的單選按鈕
Telerik UI for WPF擁有超過100個控件來創建美觀、高性能的桌面應用程序,同時還能快速構建企業級辦公WPF應用程序。UI for WPF支持MVVM、觸摸等,創建的應用程序可靠且結構良好,非常容易維護,其直觀的API將無縫地集成Visual Studio工具箱中。
Telerik_KendoUI產品技術交流群:726377843 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網