2017-08-21 59 views
0

在我的WPF應用程序,我有一個組合框,改變主題被選擇後改變:結合Telerik的WPF的主題與我的自定義樣式

private void OnThemeSelectionChanged(object sender, SelectionChangedEventArgs args) 
    { 
     var comboBox = sender as RadComboBox; 
     if (sender == null) return; 
     switch (comboBox.SelectedIndex)//@TODO - Turn to enum: 0 = Summer and etc 
     { 
      case 0: 
       SwitchToSummerTheme(); 
       break; 
      case 1: 
       SwitchToOffice2016Theme(); 
       break; 
      case 2: 
       SwitchToGreenTheme(); 
       break; 
     } 
    } 

和開關主題的方法是這樣的:

private void SwitchToGreenTheme() 
    { 
     Application.Current.Resources.MergedDictionaries.Clear(); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Data.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     AddCommonResources(); 
    } 

同爲SwitchToOffice2016Theme方法:

private void SwitchToOffice2016Theme() 
    { 
     Application.Current.Resources.MergedDictionaries.Clear(); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.Data.xaml", UriKind.RelativeOrAbsolute) 
     }); 
     AddCommonResources(); 
    } 

現在AddCommonResources方法將我自己的資源這是要包含我自己的自定義樣式詞典:在我的觀點一個

private void AddCommonResources() 
    { 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() 
     { 
      Source = new Uri("pack://application:,,,/Common;component/XamlResources/CustomResources.xaml", UriKind.RelativeOrAbsolute) 
     }); 

    } 

現在,我有一個RadRadioButton如下所示:

<telerik:RadRadioButton GroupName="a" x:Name="btn_move" 
Command="{Binding OnMoveCommand}" Content="{DynamicResource MoveString}" 
Grid.Column="5" Margin="5,3" Style="{StaticResource btn_menu_RadRadio}"/> 

現在我想要做的是:

<Style x:Key="btn_menu_RadRadio" TargetType="{x:Type telerik:RadRadioButton}" 
**BASED ON CURRENT THEME (GREEN/OFFICE2016/SUMMER)** > 
    <Setter Property="Padding" Value="1" /> 
    <Setter Property="FontWeight" Value="SemiBold" /> 
    <Setter Property="FontSize" Value="20" /> 
</Style> 

如何根據行爲實現此目的?我的意思是,我沒有資源名稱,例如:

BasedOn="{StaticResource currentTelerikTheme}" 

我該如何做到這一點?告訴WPF是基於Telerik的當前主題風格(可以是綠色/ Office2016 /夏)

回答

0

張貼在這裏我從Telerik的團隊得到了答案:

依存的名稱將保持不變跨越不同的Telerik主題。因此,您只需使用單個資源名稱即可。 要更新您的代碼,以便它是工作時主題更改,請執行下列操作:

1 - 使用支持算法FMP =「{StaticResource的RadRadioButtonStyle}」

2 - 從靜態資源改爲DynamicResource你的風格分配