Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 10-Apr-13 19:44pm
v6
Comments
Prasad Khandekar 9-Apr-13 16:18pm    
Please have a look at this thread (http://stackoverflow.com/questions/3784477/c-sharp-approach-for-saving-user-settings-in-a-wpf-application). Might be helpful in getting you started.
stibee 10-Apr-13 1:48am    
Thanks for your answer. Maybe I did not write the main problem clear, so I modified my questions again.

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.
 
Share this answer
 
Comments
stibee 11-Apr-13 1:25am    
Thanks for your answer, but the problem is this that I don't know the datacontext. I know only the grid. If user attached the my attachable property I would give user to change to color.
Francisco T. Chavez 11-Apr-13 13:47pm    
If Bindings to the DataContext are not an option, what about a Binding to a RelativeSource that's a bit higher up the VisualTree. Somewhere along the line, your grid has to be placed in some control that you can add properties too.
stibee 11-Apr-13 14:11pm    
Now I save the values in the ResourceDictionary of the grid under a specific key. And then my save mechanisme searches the saved values with secific key, and write them into the config file. After a restart i create again a new style according the saved config file. It works.. Is this is a good solution?
Francisco T. Chavez 11-Apr-13 14:16pm    
It was the first solution that went through my mind. I just figured that bindings might be easier than messing around with the resource dictionary. Your solution seems sound when I think about it, so my best guess is that it is a good solution.
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!
 
Share this answer
 
Comments
stibee 11-Apr-13 1:27am    
Thanks for your time. Maybe my question was not clear... The user should change the color over a color picker. If he choose a new color, the color should be set on grid and also saved. If he restart the application the color which he set before should be set again.
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900