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