通过WPF中的绑定动态设置资源样式
我试图使我的应用程序的配色方案动态化,以便我可以在属性中拥有一个颜色值(希望来自数据库)来确定我的应用程序的配色方案。
我有一个 Resources.xaml 文件,在其中设置应用程序的颜色和样式,然后在所有控件和窗口中使用它。我想将资源文件中 SolidColorBrush 的颜色绑定到 ViewModel 中的属性,以便该颜色可以根据当前应用程序值进行更改。这是我到目前为止所拥有的,但它不起作用,所以我一定错过了一些东西。
Resources.xaml 文件中的代码:
<SolidColorBrush x:Key="ApplicationMainBackgroundBrush" Color="{Binding Path=MainApplicationColor, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ApplicationArchitecture:ViewModelBase}, Mode=FindAncestor}, FallbackValue=CornflowerBlue}"/>
MainWindow.xaml 文件中的代码:
<Grid Grid.Row="0" x:Name="gridControl" Background="{DynamicResource ApplicationMainBackgroundBrush}">
我的 MainWindow.xaml 的 DataContext 是一个名为 ApplicationViewModel 的类,它继承自 ViewModelBase,它有一个属性“MainApplicationColor”,返回字符串“Teal”以更改SolidColorBrush 的颜色来自其 FallbackValue。我现在正在对颜色进行硬编码,但这是我希望将来从数据库中获取值的地方。颜色目前没有改变,所以我假设我的绑定源有问题,因为它显然没有像我想象的那样工作。
谢谢,
克拉拉
I am trying to make the color scheme of my application dynamic so that I can have a color value in a property(hopefully coming from the database) that determines the color scheme of my application.
I have a Resources.xaml file where I set my colors and styles for the application, which I then use throughout all my controls and windows. I would like to bind the Color of a SolidColorBrush in the resources file to a property in my ViewModel(s) so that this color can change based on the current application value. Here is what I have so far, but it isn't working so I must be missing something.
Code in the Resources.xaml file:
<SolidColorBrush x:Key="ApplicationMainBackgroundBrush" Color="{Binding Path=MainApplicationColor, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ApplicationArchitecture:ViewModelBase}, Mode=FindAncestor}, FallbackValue=CornflowerBlue}"/>
Code in the MainWindow.xaml file:
<Grid Grid.Row="0" x:Name="gridControl" Background="{DynamicResource ApplicationMainBackgroundBrush}">
The DataContext of my MainWindow.xaml is a class called ApplicationViewModel, which inherits from ViewModelBase, which has a property "MainApplicationColor" returning the string "Teal" to change the color of that SolidColorBrush from it's FallbackValue. I'm hard coding the color for now, but this is where I would like to get my value from the database in the future. The color is currently not changing, so I'm assuming there is something wrong in my binding source as it is clearly not working like I think it should.
Thanks,
Klara
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎出在您的
SolidColorBrush.Color
属性的Binding
上。祖先类型应该是
ApplicationArchitecture:MainWindow
而不是ApplicationArchitecture:ViewModelBase
。路径应包含
DataContext
。像这样......
让我知道这是否有帮助。
The problem seems to be your
SolidColorBrush.Color
property'sBinding
.There the ancestor type should be
ApplicationArchitecture:MainWindow
and notApplicationArchitecture:ViewModelBase
.The Path should include the
DataContext
in it.Like this....
Let me know if this helps.