In a XamWebGrid on Cell level (CellControlAttached event) im trying to change a cells style "in codebehind".
doing :
------------
Syle s = new Style(typeof(CellControl));
s.Setters.Add(
new Setter(CellControl.FontWeightProperty, FontWeights.Bold));
e.Cell.Style = s;
--------------
- works fine.
Question : How come doing :
new Setter(CellControl.HorizontalContentAlignmentProperty, HorizontalAlignment.Right));
-before applying the style, does not work ?
Hi SteveZ,
It is very old thread, but I am posting on it again, We are again in same situation, where a user wants to align different cells in same column, we have used following code but it makes the controls containg data to be aligned instead of data you can view this in attached image
new Setter(CellControl.HorizontalAlignmentProperty, HorizontalAlignment
.Left));
and
new Setter(CellControl.HorizontalContentAlignmentProperty, null));
new Setter(CellControl.HorizontalAlignmentProperty, HorizontalAlignment.Center));
on the other hand following code have no effect at all:
cell.Style = s;
Can you help me out if this feature hass been fixed in new versions or not at the moment we are using 2011.1
Thanks
Hi Oyvind Thanks for the purposal, although, i was not able to test or use it properly but in such case we may need n number of formating styles for each and every combinantion.
Stephen, have you any idea/comments about this approch?
thanks
I dont agree 100% with the "No" since ive done it already. But i would call my solution a "creative workaround", so the "No" is valid if we consider only straight forward "non-creative" solutions here. :)
The short story of how i did it is :
1. Use Expression Blend to pull out the CellStyle, store it in some resource file. Ive made myself a Theme.xaml file where i have it.
2. Edit this style to use a ValueConverter with the HorizontalAlignment property of the ContentPresenter :
Like this :
<ContentPresenter HorizontalAlignment="{Binding Converter={StaticResource AlignmentConverter}}" ...
- in the valueconverter have some code that returns the following depending on ure needs. :
HorizontalAlignment.Right; or ....left or center....
3. Now the trick is to read the style as "text" from a text file with a streamreader to avoid some trouble :p :
The method i use to read out the style i need looks like this :
public static Style GetCellStyle(string key) { try { Style cellStyle = null; System.Windows.Resources.StreamResourceInfo sri = Application.GetResourceStream( new Uri("Theme.xaml", UriKind.Relative)); using (System.IO.StreamReader sr = new System.IO.StreamReader(sri.Stream)) { System.Xml.Linq.XElement element = System.Xml.Linq.XElement.Parse(sr.ReadToEnd()); XName keyAttrib = XName.Get("Key", "http://schemas.microsoft.com/winfx/2006/xaml"); var style = (from f in element.Descendants() where f.Attribute(keyAttrib) != null && f.Attribute(keyAttrib).Value == key select f).FirstOrDefault(); if (style != null) { cellStyle = System.Windows.Markup.XamlReader.Load(style.ToString()) as Style; } } return cellStyle; } catch (Exception ex ) { throw ex; } }
4. Now make sure you set the datacontext of the cell to something useful so that you can code some logic to return the right thing in your valueconverter. -> In CellControlAttached do e.Cell.Control.DataContext = <something I need for my return logic>.
5. Read the style i want with whats in 3. and assign it to the cell you want to change the alignment to. Also in the CellControlAttached event.
6. Your done.
Its a creative workaround and can effect performance, but if your not to picky and can live with that it works.
Stephen : It is very relevant to be able to do this if you for example are using the grid to shape a "report" or something similar that have a high level of customization demands linked to it. In the application i currently maintain there are demands for font customization and alignment in addition to CellMerge (currently not implementet due to the grids missing support and my heads missing ability to find a good workaround) and StackedHeaders support (which i also have some very creative but not satisfying workaround for).
Hope this helps a bit on the way of getting something implemented.
Øyvind
Hi,
Currently, no.
By having that property, we override the cell's HorizontalAlignment property, so it's not possible to change via a style.
We can look into possibly making the HorizontalContentAlignment property nullable, so that if not specified we respect it on a cell level.
However, i am curious about your use case, of having different cells in the same column aligned differently. Could you explain what you're trying to do?
Thanks,
-SteveZ
Hi Stephen,
I used following code on column and it works fine for the complete column, but I want to apply Different Alignment on individual cells of a column.
<igGrid:TextColumn Key="Col1" HorizontalContentAlignment="Center"> <igGrid:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="Col1"/> </DataTemplate> </igGrid:TextColumn.HeaderTemplate> </igGrid:TextColumn>
Is there any way to apply different cell alignment in a single column like conditional formating demo example (that applied different cell format on individual cells).