Welcome to the Xceed Community Sign in | Join | Help
Community Search  

Set the format string for a column at runtime.

Sort Posts: Previous Next
  •  05-28-2008, 10:02 AM Post no. 12544

    Set the format string for a column at runtime.

    Good day.

    I am looking for a way to set a format string for a grid column,

    eg. "n2" for 2 decimal values ("1,324.23") or p0 for a percentage with 0 decimals ("34%") and so on.

    I do not want to do it in XAML as I can bind any data table to the grid.

    Thank you.

  •  05-28-2008, 10:54 AM Post no. 12546 in reply to 12544

    Re: Set the format string for a column at runtime.

    The Formatting a cell's content example in the Template Elements topic demonstrates how to format a cell's content by create a new CellContentTemplate (DataTemplate) in which the binding on the Text property of the TextBlock that is used to display the cell content uses an IValueConverter to change how a cell's content is displayed.


    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
  •  05-30-2008, 7:36 AM Post no. 12582 in reply to 12546

    Re: Set the format string for a column at runtime.

    I am getting sick and tired of WPF.

    YES you can do a lot of things in XAML but what if you need to do it in code behind?

    What if I know that column "X" needs to be formated using format string "n2" or "p2" or whatever. What if I want to change its content alignment to Right instead of left. What what what.

    I do not want to build one screen and bind it to one table and use loads of XAML to make it look pretty.

    I need to build one generic datagrid that is bound to the output from the database. I have classes that tells me how to format the columns in those tables. Now I am simply trying to do that.

    With WPF component developers seem to have forgotten developers. Sure a designer can go sit for a day and make one screen look good, but what if you want to do it genericly?

    After a full day of figuring out how to manipulate data templates in code behind I finally managed to apply my basic format string to a cell.

     

    For those that wonder here is how: (c is a class that contains all the information on how the column should look)

    Dim xc As Xceed.Wpf.DataGrid.Column
    xc = New Xceed.Wpf.DataGrid.Column
    xc.FieldName = c.ColumnName
    xc.Visible = True
    xc.Title = c.DisplayName

    Me.GenericXceedGrid.Columns.Add(xc)

    Dim itemsBinding As Binding = New Binding()
    itemsBinding.Converter = New GridConverter
    itemsBinding.ConverterParameter = c.DisplayFormat

    Dim textBlockFactory As New FrameworkElementFactory(GetType(TextBlock))
    textBlockFactory.Name = "myTextBlockFactory"
    textBlockFactory.SetBinding(TextBlock.TextProperty, itemsBinding)
    textBlockFactory.SetValue(TextBlock.HorizontalAlignmentProperty, c.HorizontalAlignment)

    xc.CellContentTemplate = New DataTemplate
    xc.CellContentTemplate.VisualTree = textBlockFactory

     

    and GridConverter:

    <ValueConversion(GetType(Object), GetType(String))> _
    Public Class GridConverter
        Implements IValueConverter

        Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
            If value Is Nothing Or IsDBNull(value) Or parameter Is Nothing Or parameter = "" Then
                Return value
            End If
            Return Format(value, parameter)
        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
            Return CType(value, Object)
        End Function
    End Class 

    See? No XAML.

    Now just imagine how cool it would have been if all I needed to do was:

    xc.FormatString = c.DisplayFormat

    But no. Do it in XAML.

    Now here is my next stumbling block. I want to align the TITLE...

    Next thing I am going to mess around with the statistics alignment and formatting. I shudder to think how long that is going to take me.

    I am sorry to burst out here and have my little rant. It is just that I have had the misfortune of having to implement WPF in our project and even though Xceed is the best component by far it still fails me.

    So for v2.3 on the roadmap: Please please please help the poor developers that need to do things in code, not XAML.

    Thanks Indifferent

  •  05-30-2008, 9:01 AM Post no. 12589 in reply to 12582

    Re: Set the format string for a column at runtime.

    First and foremost, I think that each and every WPF developer feel your pain. We all have been through the same type of issues when doing the WPF transition and we understand what you mean.

    That being said, WPF is a framework where XAML and Code-Behind are meant to co-exist and interact together. It is my opinion that WPF should not be considered or used without its XAML part. One of the best example is FrameworkElementFactory; this class is documented as Obsolete and you will never be able to achieve complex Templates with it. Also, a relatively simple template or style can take dozens of lines in Code-Behind but only 5 in XAML.

    I agree with you that most examples and documentation topics assume a high level of static architecture, but a point I want to make is that it is up to you to draw the line of where XAML start and where it ends. Nothing forces you to define the window layout in XAML, it is just a possibility, which happens to be what most people with static UI do.

    Building UI dynamically is more than possible by mixing both VB and XAML, you can easily create controls and place them in other controls in code-behind, but create the styles and templates in XAML. You can then load the styles and templates in code and assign them to the controls created dynamically.

    So, my advice as a fellow developer who once had to climb the same WPF cliff (and fell down a few times) is: Don't ignore XAML or pretend it doesn't exist. You just have to find the proper balance.

    Hang in there!!!




    Marc Laroche
    Software Developer
    Xceed Software Inc.


    I don’t suffer from insanity, I enjoy every minute of it. - Unknown
  •  05-30-2008, 9:07 AM Post no. 12590 in reply to 12589

    Re: Set the format string for a column at runtime.

    I smile as I read your post.

    I am really having a love hate relationship with WPF at the moment.

    But the power of WPF is amazing. I just hate spending an entire day on something so simple...

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.