This is a migrated thread and some comments may be shown as answers.

How to apply StyleSelector to DataFormatString

5 Answers 124 Views
GridView
This is a migrated thread and some comments may be shown as answers.
david mcintyre
Top achievements
Rank 1
david mcintyre asked on 04 Aug 2010, 04:05 PM
Whenever I attempt to apply a style selector to dynamically set the DataFormatString, I get a xamlparseexception.  I would like to dynamically set the # of decimal places in the cells' data. 
My style selector is declared in the usercontrol's resources as:
<local:CellDataFormatStyleSelector x:Key="myDataformatStyleSelector"/>
and is applied in my data column as:
<telerikGridView:GridViewDataColumn Header="1" DataMemberBinding="{Binding PeriodQty1}"   TextAlignment="Right" HeaderTextAlignment="Center" CellStyleSelector="{StaticResource myDataformatStyleSelector}"     />
Here is the code for my style selector:
public class CellDataFormatStyleSelector : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            var style = new Style(typeof(GridViewDataColumn));
            //var row = (GridViewRow)container;
            var obj = (ItemLocationperiodDemand)item;
            var col = (GridViewCell)container;
            if (obj.YearLabel == ApplicationStrings.Composite)
            {
                style.Setters.Add(new Setter(GridViewDataColumn.DataFormatStringProperty, "{}{0:n1}"));
            }
            else
            {
                style.Setters.Add(new Setter(GridViewDataColumn.DataFormatStringProperty, "{}{0:n0}"));
            }
            return style;          }
    }
In my styleselector code, the type of the container is GridViewCell.  But I can't set a DataFormatString property within a style for a cell, only within the datacolumn.  So is it possible to do this with a style selector? 

5 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 09 Aug 2010, 08:27 AM
Hello,

 Why not use CellTemplateSelector instead? You can load completely different templates depending on your condition. Please check this demo for more info. 

Best wishes,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
david mcintyre
Top achievements
Rank 1
answered on 09 Aug 2010, 05:20 PM
I didn't use the CellTemplateSelectors because the DataTemplate property includes the name of the property that's being bound to.  And I need to apply the logic as to how the column's cells are styled in 13 of the grid's columns.  So if i use the template selector, then I have to create 13 sets of selectors and templates  (unless there's a way to bind to the paren'ts datamemberbinding in the datatemplate).
0
Vlad
Telerik team
answered on 10 Aug 2010, 06:31 AM
Hello ,

 You can construct the template dynamically similar to my blog post. Simple string.Format may do the work with dynamic binding in your case. 

All the best,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
david mcintyre
Top achievements
Rank 1
answered on 13 Aug 2010, 08:34 PM
Your blog post doesn't seem to be a convenient solution to my problem.  I have 13 different columns in my datagrid, and each needs to have dataformatstring dynamically applied.  And each column is bound to a different property.  But in the template example in your blog post, the binding source property is specified in the template
<TextBlock Text=""{Binding ID}""

So how can i create a template that can be bound to any property?
0
Vlad
Telerik team
answered on 16 Aug 2010, 06:54 AM
Hello,

 As I said in my previous reply you need to construct the string (XAML) dynamically. For example:
string xaml = String.Format(@"<TextBlock Text=""{{Binding {0}}}""", "YourProperty");

Sincerely yours,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
david mcintyre
Top achievements
Rank 1
Answers by
Vlad
Telerik team
david mcintyre
Top achievements
Rank 1
Share this question
or