翻譯|使用教程|編輯:龔雪|2022-05-06 10:45:31.953|閱讀 195 次
概述:本文主要為大家介紹如何使用Telerik WPF控件在運(yùn)行時(shí)切換主題,歡迎下載最新版體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
通過(guò)使用帶有隱式樣式的主題機(jī)制,您可以在運(yùn)行時(shí)更改Telerik UI for WPF控件的主題,無(wú)需重新創(chuàng)建UI。需要做的就是刪除當(dāng)前合并的詞典,然后在代碼隱藏中將另一個(gè)主題的合并詞典添加到您的應(yīng)用程序資源中:
示例 1:在代碼中合并資源字典
C#
Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = ......});
這將在運(yùn)行時(shí)將不同的隱式樣式應(yīng)用于您的控件。
在這篇幫助文章中,我們將通過(guò)一個(gè)快速示例來(lái)演示該方法。
1. 從位于控件安裝文件夾中的 Binaries.NoXaml 文件夾中添加所需的程序集,您還必須包括主題程序集。
2. 在 App.xaml 中為默認(rèn)主題添加所需的資源字典。
示例 2:在 XAML 中合并 ResourceDictionaries
XAML
<Application.Resources> <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> </Application.Resources>
3. 將您選擇的一些控件添加到頁(yè)面中。 在這個(gè)例子中,我們將添加一個(gè) Grid、StackPanel、RadComboBox、RadDateTimePicker 和三個(gè) RadButton 來(lái)在三個(gè)主題之間切換。
示例 3:定義視圖
XAML
<Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal" Background="#FFE5E5E5" HorizontalAlignment="Stretch"> <telerik:RadButton Content="Office Black" VerticalAlignment="Center" Width="110" Margin="10" Click="OfficeBlack_Click" /> <telerik:RadButton Content="Windows8" VerticalAlignment="Center" Width="110" Margin="10" Click="Windows8_Click" /> <telerik:RadButton Content="Windows 7" VerticalAlignment="Center" Width="110" Margin="10" Click="Windows7_Click" /> </StackPanel> <StackPanel Orientation="Vertical" Grid.Row="1" Margin="20" HorizontalAlignment="Left"> <telerik:RadComboBox Width="230" Margin="10"> <telerik:RadComboBoxItem Content="Item 1" /> <telerik:RadComboBoxItem Content="Item 2" /> <telerik:RadComboBoxItem Content="Item 3" /> <telerik:RadComboBoxItem Content="Item 4" /> <telerik:RadComboBoxItem Content="Item 5" /> </telerik:RadComboBox> <telerik:RadDateTimePicker Width="230" Margin="10" IsDropDownOpen="True" /> </StackPanel> </Grid>
4. 該示例將使用最簡(jiǎn)單的方法在運(yùn)行時(shí)更改主題 - 它將使用三個(gè)按鈕中的每一個(gè)的 Click 事件。 單擊后,我們將從應(yīng)用程序資源中清除合并的字典,并從主題程序集中合并新的資源字典。
示例 4:在運(yùn)行時(shí)合并主題資源
C#
private void OfficeBlack_Click(object sender, RoutedEventArgs e) { Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)}); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)}); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)}); } private void Windows8_Click(object sender, RoutedEventArgs e) { Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Windows8;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)}); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)}); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)}); } private void Windows7_Click(object sender, RoutedEventArgs e) { Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Windows7;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)}); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)}); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)}); }
5. 圖 1-3 顯示了最終結(jié)果。
圖 1:?jiǎn)螕?Office Black 按鈕來(lái)顯示黑色和灰色主題。
圖 2:Windows8 主題為所有控件顯示了一組不同的顏色——Windows 8 的平面樣式。
圖 3:最后,Windows7 主題為控件顯示了一組藍(lán)灰漸變色。
Telerik UI for WPF擁有超過(guò)100個(gè)控件來(lái)創(chuàng)建美觀、高性能的桌面應(yīng)用程序,同時(shí)還能快速構(gòu)建企業(yè)級(jí)辦公WPF應(yīng)用程序。UI for WPF支持MVVM、觸摸等,創(chuàng)建的應(yīng)用程序可靠且結(jié)構(gòu)良好,非常容易維護(hù),其直觀的API將無(wú)縫地集成Visual Studio工具箱中。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)