将WPF窗口背景设置为资源字典画笔用户设置
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将需要几个步骤。
您将需要一个转换器,因为您无法绑定 StaticResource 或 DynamicResource 的 x:Key。为了使转换器能够轻松访问资源,应将它们添加到应用程序级别
ApplicationResourceKeyConverter
然后您可以将 MainWindow Background 属性绑定到用户设置字符串 MainBackground ,例如
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
ApplicationResourceKeyConverter
Then you can bind the MainWindow Background property to the user settings string MainBackground like
不使用 DynamicResource,只需让用户选择并设置背景,或者使用名为 UserChosenColor 的属性并将背景绑定到该属性。
您还可以使用将字符串转换为画笔的转换器来绑定到设置 (MainBackground) 中的属性。
编辑
由于用户更改了他的问题作为将每个窗口设置为选定背景的方法,因此它也很简单。使用 setter 定义样式,如下所示:
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: