我想完成的是允許我的用戶通過從列表中選擇主色來更改我的應用程序的整體顏色主題。我有這個工作,但有一些我注意到,如果我能弄清楚如何去做,可能會讓我的生活更輕鬆。現在我有一個看起來像這樣的主題文件夾。包含其他文件夾和常用資源字典的主題文件夾(即用於背景的畫筆)。下一組文件夾是RedTheme,BlueTheme文件夾,每個文件夾都包含用於單獨控制的資源字典;例如BlueTheme有一個按鈕的資源字典,RedTheme也有。我注意到有趣的事情是,資源字典幾乎與只有一個SolidColorBrush的顏色例外相同。所以我試圖以某種方式綁定這個筆刷,這樣我就不會在同一個資源字典的副本上發現副本,只有一個區別。我會盡我所能來放入我的代碼,但它的本質很複雜,實際上不能看到不同的字典。另外我應該補充一點,我在嵌套字典中嵌套字典。 App.xaml包含任何包含多個字典本身的字典,換入或換出主題更改和一些通用字典。使用嵌套資源字典動態更改wpf中的主題
ThemeBlue.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="BlueTheme/BlueBrushes.xaml"/>
<ResourceDictionary Source="BlueTheme/ButtonStyleAndTemplate(normal_blue).xaml"/>
<ResourceDictionary Source="BlueTheme/LabelStyleAndTemplate(normal_blue).xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
ThemeRed.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="RedTheme/RedBrushes.xaml"/>
<ResourceDictionary Source="RedTheme/ButtonStyleAndTemplate(normal_red).xaml"/>
<ResourceDictionary Source="RedTheme/LabelStyleAndTemplate(normal_red).xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
這就是我想要的動態變化發生 我只是想刷某處綁定在選擇新主題時隱藏代碼,並將該筆刷彈出到我試圖突出顯示的位置,所以我只需要爲每種顏色生成一個這樣的字典,而不是一個字典。
Style TargetType="{x:Type mycon:MyButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type mycon:MyButton}">
<Grid Margin="2">
<Ellipse Name="MainCircle" Fill="{DynamicResource ResourceKey=TranslucentBrush}" Stroke="{DynamicResource ResourceKey=***RedBorderBrush***}" />
<Ellipse Name="RefractionCircle" Fill="{DynamicResource ResourceKey=ButtonRefractionLayer}"/>
<mycon:ButtonTextBlock Margin="0,0,0,8" FontWeight="Black" FontSize="20" Foreground="{DynamicResource ResourceKey=***RedBorderBrush***}" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Path x:Name="ReflectionLayer" VerticalAlignment="Top" Stretch="Fill">
<Path.RenderTransform>
<ScaleTransform ScaleY="0.5" />
</Path.RenderTransform>
<Path.Data>
<PathGeometry>
<PathFigure IsClosed="True" StartPoint="98.999,45.499">
<BezierSegment Point1="98.999,54.170" Point2="89.046,52.258" Point3="85.502,51.029"/>
<BezierSegment IsSmoothJoin="True" Point1="75.860,47.685" Point2="69.111,45.196" Point3="50.167,45.196"/>
<BezierSegment Point1="30.805,45.196" Point2="20.173,47.741" Point3="10.665,51.363"/>
<BezierSegment IsSmoothJoin="True" Point1="7.469,52.580" Point2="1.000,53.252" Point3="1.000,44.999"/>
<BezierSegment Point1="1.000,39.510" Point2="0.884,39.227" Point3="2.519,34.286"/>
<BezierSegment IsSmoothJoin="True" Point1="9.106,14.370" Point2="27.875,0" Point3="50,0"/>
<BezierSegment Point1="72.198,0" Point2="91.018,14.466" Point3="97.546,34.485"/>
<BezierSegment IsSmoothJoin="True" Point1="99.139,39.369" Point2="98.999,40.084" Point3="98.999,45.499"/>
</PathFigure>
</PathGeometry>
</Path.Data>
<Path.Fill>
<RadialGradientBrush GradientOrigin="0.498,0.526">
<RadialGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1" ScaleY="1.997"/>
<TranslateTransform X="0" Y="0.5"/>
</TransformGroup>
</RadialGradientBrush.RelativeTransform>
<GradientStop Offset="1" Color="#99FFFFFF"/>
<GradientStop Offset="0.85" Color="#72FFFFFF"/>
<GradientStop Offset="0" Color="#00000000"/>
</RadialGradientBrush>
</Path.Fill>
</Path>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="MainCircle" Property="Fill" Value="{DynamicResource ResourceKey=TransparentBrush}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="MainCircle" Property="Fill" Value="{DynamicResource ResourceKey=HighlitTranslucentBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- EndRegion -->
</ResourceDictionary>
-------代碼隱藏主窗口----------
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SetDefaultStyle();
populateCboTheme();
}
private void populateCboTheme()
{
cboTheme.Items.Add("Red");
cboTheme.Items.Add("Blue");
cboTheme.Items.Add("Green");
cboTheme.Items.Add("Yellow");
}
private void SetDefaultStyle()
{
SetNewStyle("Red");
}
private void SetNewStyle(string _color)
{
Uri themeUri = new Uri(string.Format("Themes/Theme{0}.xaml",_color),UriKind.Relative);
ResourceDictionary theme = (ResourceDictionary)Application.LoadComponent(themeUri);
Resources.MergedDictionaries.Add(theme);
}
private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void cboTheme_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (cboTheme.SelectedIndex)
{
case 0:
SetNewStyle("Red");
break;
case 1:
SetNewStyle("Blue");
break;
case 2:
SetNewStyle("Green");
break;
case 3:
SetNewStyle("Yellow");
break;
}
}
}
問題如何?是的,你發佈了很多文字,但問題並不清楚。有什麼具體問題嗎?也許你應該把你的帖子壓縮到關鍵部分,這樣每個人都願意完全閱讀它。 – DHN 2013-03-08 08:16:29
它發生在我身上。問題是,如何將顏色或筆刷直接綁定到資源字典上? – MegaMark 2013-03-08 15:47:59