在运行时更改样式以及如何在重新启动时设置此新样式(如何保存) [英] Change style at runtime and how to set this new style at restarting again (how to save)

查看:70
本文介绍了在运行时更改样式以及如何在重新启动时设置此新样式(如何保存)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



主要问题:在运行时通过上下文菜单更改网格样式,保存并设置应用程序再次启动的情况。



详细:

- 我在resourceDictionary中有每个网格的样式。这个字典被添加到Application.Current.Resources.MergedDictionaries。

- 我有一个可附加的属性用于这种网格。如果你将它设置为true,它会点击网格并添加一些额外的功能(目前只在上下文菜单中启用/禁用列)

- 所有设置(顺序,过滤器,...... 。)如果用户将另一个可附加属性添加到网格,将在每次更改时保存到配置文件中。 (它也可以轻拍网格并监听不同类型的chaning事件)。对于每个网格,它在此配置文件中都有一个具有特定设置的部分。在重新加载应用程序时,属性再次设置为网格。



现在我需要在此上下文菜单中更改为前景和表格内容的背景颜色在运行时。重新启动后,所选颜色应该像之前设置的那样(我不知道datacontext)。

我的方法是创建在上下文菜单中添加颜色选择器,如果是新颜色我选择了新的颜色,如下所示:





Hello

Main Problem: Change style of a grid at runtime over a contextmenu, save it and set in the case the application starts again.

Detailed:
- I have a style for each grid inside a resourceDictionary. This dictionary is added to Application.Current.Resources.MergedDictionaries.
- I have an attachable property for this kind of grid grid. If you set this to true, it taps the grid and add some additional functionalities (at the moment only enable/disable of columns over context menu)
- All the settings (order, filter,.... ) will be saved on each change into a config file, if the user adds another attachable property to the grid. (It taps the grid also and is listend to different kinds of chaning events). For each grid it has a section with specific setting inside this config file). At reloading of the application the properties are set to the grid again.

Now I have the requirement to change to foreground and the background color of the table content over this contextmenu at runtime. After a restart the selected colors should be like set before (I don't know the datacontext).
My approach was to create to add a colorpicker in the context menu, and in the case a new color is selected I create a new style with the new color like this:


    <br />
localGridStyle = new Style(typeof(XY));<br />
    <br />
    _localGridStyle.Setters.Add(new Setter<br />
    {<br />
      Property = XY.BackgroundProperty,<br />
      Value = _selectedBackground<br />
    });<br />
this.grid.Resources.Add(typeof(XY), _localGridStyle);<br />
<br />





随着这个解决方案我可以改变颜色,网格会超过新设定的颜色。

问题是,如何保存这种风格并在重启后再次加载?

我错了完整吗?





谢谢



With this solution I can change the color and the grid overtakes the new set colors.
The problem is, how can I save this style and load again after restart?
Am I wrong complete?


Thanks

推荐答案

对不起,但我不确定是否真的需要为两个不同的画笔设置颜色。如果我在你的鞋子里,我会在你的网格的DataContext对象中创建两个Brush属性,然后绑定到它。这样,你可以在运行时设置你想要的任何刷子,而不会有太多麻烦。
I'm sorry, but I'm not certain that creating new styles is really required for setting the colors for two different brushes. If I were in your shoes, I would create two Brush properties in the DataContext object of your grid, and bind to that. This way, you can set whatever Brush you want at runtime without too much trouble.






我还没试过这个逻辑,但我相信它可能通过计时器,首先你必须定义不同的风格和页面lode功能设置风格1然后经过一段时间后风格随机改变。

最好的运气!
Hi,

I had not tried this logic but but i believe that it may possible through timer, at first you have to define different styles and in page lode function set style 1 then after a time span the style change randomly.
Best of Luck!


这里我给你的想法。



1)创建属性并实现INotifyPropertyChanged接口



2)创建一个注册表来存储所选颜色



3)读取颜色并在注册表中设置颜色并设置值在物业



4)使用您的物业绑定网格颜色:



背景={Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type Window}},Path = ColorValue}>



这里ColorValue是在课堂上定义的属性。



如果你需要完整的解决方案,我可以给你或邮寄你的示例项目。



谢谢
Here i just give you the idea.

1) Create Property and implement INotifyPropertyChanged interface

2) Create a registry to store the selected color

3) read the color and set the color in registry and set value in Property

4) Bind Grid Color with your property using :

Background="{Binding RelativeSource={RelativeSource FindAncestor ,AncestorType={x:Type Window}},Path=ColorValue}">

Here ColorValue is property defined in class.

If you need complete solution i can give you or mail you sample project .

Thanks


这篇关于在运行时更改样式以及如何在重新启动时设置此新样式(如何保存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆