将WPF窗口背景设置为资源字典画笔用户设置

发布于 2024-11-24 08:21:03 字数 1019 浏览 1 评论 0原文

我在 ResourceDictionary 中声明了两个画笔,我希望用户选择他们想要在主窗口上看到的背景。

资源词典画笔:
x:Key="LightBlueMainWindow"
x:Key="DarkBlueMainWindow"

窗口:
Background="{DynamicResource LightBlueMainWindow}">

我有一个项目用户设置定义了“MainBackground”,它是一个字符串,可以包含任一键(LightBlueMainWindow 或 DarkBlueMainWindow)。

根据 XAML 中的用户设置动态设置背景的最佳方法是什么?

编辑

我需要提及的是,我需要从整个应用程序中的许多不同的用户控件和窗口访问此画笔。我不想在我想设置此画笔的每个地方都设置属性。

此外,画笔是预先定义的,而不仅仅是像这样的颜色

<LinearGradientBrush x:Key="LightBlueMainWindow" EndPoint="0.5,1" 
                     MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
    <LinearGradientBrush.GradientStops>            
        <GradientStopCollection>
            <GradientStop Color="#FFE9EFF3" />
            <GradientStop Color="#FF84A1B8" Offset="1"/>
        </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

I have two brushes declared in my ResourceDictionary and I would like the user to select which background they want to see on the main window.

Resource Dictionary Brushes:
x:Key="LightBlueMainWindow"
x:Key="DarkBlueMainWindow"

Window:
Background="{DynamicResource LightBlueMainWindow}">

I have a project User Setting defined 'MainBackground' which is a string and can contain either key (LightBlueMainWindow or DarkBlueMainWindow).

What is the best way to dynamically set the background based on the user setting in XAML?

EDIT

I need to mention that I need to access this brush from many different user controls and windows throughout the application. I don't want to set a property on every single place I want to set this brush.

Also, the brushes are pre-defined and not just a color like this

<LinearGradientBrush x:Key="LightBlueMainWindow" EndPoint="0.5,1" 
                     MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
    <LinearGradientBrush.GradientStops>            
        <GradientStopCollection>
            <GradientStop Color="#FFE9EFF3" />
            <GradientStop Color="#FF84A1B8" Offset="1"/>
        </GradientStopCollection>
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千秋岁 2024-12-01 08:21:03

这将需要几个步骤。

您将需要一个转换器,因为您无法绑定 StaticResource 或 DynamicResource 的 x:Key。为了使转换器能够轻松访问资源,应将它们添加到应用程序级别

<Application ...>
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="BrushesDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <local:ApplicationResourceKeyConverter x:Key="ApplicationResourceKeyConverter"/>
        </ResourceDictionary>
    </Application.Resources>
</Application>

ApplicationResourceKeyConverter

public class ApplicationResourceKeyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string key = value as string;
        return Application.Current.TryFindResource(key);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后您可以将 MainWindow Background 属性绑定到用户设置字符串 MainBackground ,例如

<Window ...
        xmlns:ProjectProperties="clr-namespace:YourProjectName.Properties" 
        Background="{Binding Source={x:Static ProjectProperties:Settings.Default},
                             Path=MainBackground,
                             Converter={StaticResource ApplicationResourceKeyConverter}}">
    <!--...-->
</Window>

It will require a couple of steps

You'll need a converter since you can't bind x:Key of a StaticResource or DynamicResource. For the converter to be able to get easy access to the resources they should be added at appliaction level

<Application ...>
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="BrushesDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <local:ApplicationResourceKeyConverter x:Key="ApplicationResourceKeyConverter"/>
        </ResourceDictionary>
    </Application.Resources>
</Application>

ApplicationResourceKeyConverter

public class ApplicationResourceKeyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string key = value as string;
        return Application.Current.TryFindResource(key);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Then you can bind the MainWindow Background property to the user settings string MainBackground like

<Window ...
        xmlns:ProjectProperties="clr-namespace:YourProjectName.Properties" 
        Background="{Binding Source={x:Static ProjectProperties:Settings.Default},
                             Path=MainBackground,
                             Converter={StaticResource ApplicationResourceKeyConverter}}">
    <!--...-->
</Window>
究竟谁懂我的在乎 2024-12-01 08:21:03

不使用 DynamicResource,只需让用户选择并设置背景,或者使用名为 UserChosenColor 的属性并将背景绑定到该属性。

您还可以使用将字符串转换为画笔的转换器来绑定到设置 (MainBackground) 中的属性。

编辑

由于用户更改了他的问题作为将每个窗口设置为选定背景的方法,因此它也很简单。使用 setter 定义样式,如下所示:

<!-- Window style -->
<Style TargetType="{x:Type Window}">
    <Setter Property="Background" Value="{Binding MainBackground, Mode=OneWay, Converter=StringToBrushConverter}"/>
</Style>

Instead of using DynamicResource, just have the user pick and set the background, or have a property called UserChosenColor and bind the Background to that.

You could also bind to the property in your settings (MainBackground) by using a converter that converted a string to a Brush.

Edit

Since the user changed his question as a method of setting every window to a chosen background, it's also simple. Define a style with a setter like this:

<!-- Window style -->
<Style TargetType="{x:Type Window}">
    <Setter Property="Background" Value="{Binding MainBackground, Mode=OneWay, Converter=StringToBrushConverter}"/>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文