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,
The HorizontalContentAlignment and VerticalContentAlignment properties are actually dictated by the Column. So, no matter what you set the property to be on an individual cell, it will go back to what the column dictates.
Column.HorizontalContentAlignment, Column.VerticalContentAlignment.
-SteveZ
Does this mean it is not possible to set different alignment for the content on different rows in the same column in XamWebGrid ?
In general we have quite high demands for customization of "look and feel" all the way down to Cell level in the app we are creating. I have earlier asked about FormatString on a cell level and gotten a reply that this is not supported on a lower level then Column, and now these properties are not supported on a lower level then column either ?
- Can you please comment on this with respect to what we can expect from coming versions of this grid component when it comes to freedom to style on Column, Row and Cell level. (Including mentioned FormatString, and HorizontalContentAlignment, VerticalContentAlignment, etc etc )
- Can you also comment on the plans for implementing a cell merge function. ( which we also need badly)
Having full freedom to style on independent Column, Row and Cell level is for us so important that its the deciding point when it comes to choosing component to work with for the future. Knowing this is the first version of the grid compoent on the Silverlight platform makes us ofcourse more patient when it comes to missing functionality, but it would be nice with some "roadmap" indications when it comes to these details.
regards
Oyvind Johnsen
Hi Oyvind ,
Its still possible. You'd just set the HorizontalContentAlignment to Stretch, and if you're using a TemplateColumn, you'd set the HorizontalAlignment of the root element in your Template to which ever direction. If you're using something such as a TextColumn, you'd customize the TextBlockStyle, and set TextAlignment to the direction you'd like.
As, for styling Cells, you can do so on the Cell, Row, and Column level. However, with ContentAlignment, for ease of use, we gave full control to the Column.
Cell Merging is on our "to do" list, however will not be in the 9.2 release.
Hi Stephen,I've grid like following <igGrid:XamWebGrid x:Name="g" AutoGenerateColumns="False" IsAlternateRowsEnabled="true" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible"> <igGrid:XamWebGrid.Columns> <igGrid:TextColumn Key="Col1"> <igGrid:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text=" Col1"/> </DataTemplate> </igGrid:TextColumn.HeaderTemplate> </igGrid:TextColumn> <igGrid:TextColumn Key=" Col2"> <igGrid:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text=" Col2"/> </DataTemplate> </igGrid:TextColumn.HeaderTemplate> </igGrid:TextColumn> </igGrid:XamWebGrid.Columns> <igGrid:XamWebGrid.CellStyle> <Style TargetType="igGrid:CellControl"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> </igGrid:XamWebGrid.CellStyle>
</igGrid:XamWebGrid>
this style is applied on button click:
s.Setters.Add(new Setter(CellControl.HorizontalAlignmentProperty, HorizontalAlignment.Center));
it make cell control center align instead of text.
Can you please help me to reolve this issue?
Thanks
You can use the HorizontalContentAlignment property on your Column to control the HorizontalAlignment of a cell's content.
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).
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,
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
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