views:

253

answers:

1

Hi;

sorry if I'm not clear, it's not really clear in my head too (especially after trying to find my way in other posts :p)

What I'm willing to do is creating DataGrids in code, with zero to numerous columns containing a button, which will call one same function but with a "parameter" (different for each column).

Here is what I got so far : DataGrid created in code DataTemplate defined in the xaml resources (with a button) DataGridTemplateColumn which uses the above DataTemplate

Is it possible to bind the button's properties (in the DataTemplate), to the DataGridTemplateColumn properties (in my case, the column header would be ok), and how?

Is there a way to get an access to the DataTemplate components (the button for example) in code, and modify their properties?

Is it possible (and not hazardous) to create a DataTemplate in code? I declared mine in xaml because I found a post advising to do so instead of code.

Thanks for your help.

A: 

Chouppy

you can let the XamlReader do the work:

   oDataTemplate = TryCast(System.Windows.Markup.XamlReader.Load(New System.Xml.XmlTextReader(New System.IO.StringReader(sXaml))), DataTemplate)

sXaml my be bould like this (in my case this is a usercontrol containing some checkboxes, which are bound to datatable fields):

  Dim sDelim As String = vbNewLine
  Try
     sXaml = "<DataTemplate " & sDelim
     sXaml = sXaml & " xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""" & sDelim
     sXaml = sXaml & " xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""" & sDelim
     sXaml = sXaml & " xmlns:local=""clr-namespace:Infor.Blending.Admin.Client;assembly=Infor.Blending.Admin.Client""" & sDelim
     sXaml = sXaml & " xmlns:dg=""http://schemas.microsoft.com/wpf/2008/toolkit""" & sDelim
     sXaml = sXaml & " >" & sDelim
     sXaml = sXaml & " <local:RightEditor Tag=""Collapsed""" & sDelim
     sXaml = sXaml & " Amend=""{Binding Path=Item.Right0, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dg:DataGridRow}}}""" & sDelim
     sXaml = sXaml & " Create=""{Binding Path=Item.Right1, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dg:DataGridRow}}}""" & sDelim
     sXaml = sXaml & " Delete=""{Binding Path=Item.Right2, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dg:DataGridRow}}}""" & sDelim
     sXaml = sXaml & " Review=""{Binding Path=Item.Right3, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dg:DataGridRow}}}""" & sDelim
     sXaml = sXaml & " />" & sDelim
     sXaml = sXaml & " </DataTemplate>"

Finally you can set the datatemplate:

 Dim oTemp As DataGridTemplateColumn = Nothing
       oTemp.CellTemplate = oDataTemplate
klawusel