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

[Solved] Dynamically binding data template

2 Answers 407 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ivo
Top achievements
Rank 1
Ivo asked on 23 Apr 2009, 03:12 PM
Hi,
I need to generate a grid dynamically, and add a CellEditorTemplate based on the type of data.
I'm currently doing this on Xaml:
<UserControl.Resources>
        <res:Resource x:Key="ResourcesList"/>
        <DataTemplate x:Key="intEdit">
                <Grid>
                    <telerikInput:RadNumericUpDown Maximum="1000000" Value="{Binding}"></telerikInput:RadNumericUpDown>
                </Grid>
        </DataTemplate>
        <DataTemplate x:Key="stringEdit">
                <Grid>
                    <TextBox Text="{Binding}"></TextBox>
                </Grid>
            </Border>
        </DataTemplate>
 </UserControl.Resources>
    <Grid x:Name="LayoutRoot">
        <telerikGrid:RadGridView x:Name="tableEditorGrid" AutoGenerateColumns="False" Height="300"></telerikGrid:RadGridView>
    </Grid>

And in codebehind doing something like this:
                GridViewDataColumn colName = new GridViewDataColumn();
                colName.HeaderText = property.Name;
                Binding bd =  new Binding(property.Name);
                bd.Mode =BindingMode.TwoWay;
                colName.DataMemberBinding = bd;
                tableEditorGrid.Columns.Add(colName);

                if (property.ScalarType.NativeType.Contains("Int"))
                {
                    colName.CellEditTemplate = this.Resources["intEdit"] as DataTemplate;
                }
                else
                {
                    colName.CellEditTemplate = this.Resources["stringEdit"] as DataTemplate;
                }
What it happens is that the property doesen't get bind to the data template, and to work I need to explicity define the binding in the datatemplate:(
Suppose that property.Name is  something like "Phone" then in order for this to work i need explicity define like this
    <DataTemplate x:Key="stringEdit">
                <Grid>
                    <TextBox Text="{Binding Phone, Mode=TwoWay}"></TextBox>
                </Grid>
            </Border>
        </DataTemplate>
But I want that the  data template  for the CellEditTemplate to get automaticaly binded to the GridViewDataColumn DataMemberBinding.Is this possible?
I'm sorry if this turn out be confusing but I couldn't find any other way to expose my problem.
Thanks,
Ivo

2 Answers, 1 is accepted

Sort by
0
BOARD LAB
Top achievements
Rank 1
answered on 23 Apr 2009, 07:47 PM
Ivo,
I had the same problem (if I understand correctly your question).
As you probably discovered, in Silverlight you cannot create a DataTemplate with code but only in XAML so it's difficult to create a template dynamically.
The solution I found is to create a method like this (sorry if there are mistakes, I don't have the actual code on hand right now).
Then in your column definition you set the CellTemplate to the result of this method.
It's stupid but it works.
public DataTemplate CreateTemplate(string propName) 
return XamlReader.Load(@"<DataTemplate><TextBlock Text="""{Binding " + propName + "}"""</DataTemplate>") as DataTemplate; 
 
Hope it helps.
0
Ivo
Top achievements
Rank 1
answered on 24 Apr 2009, 08:19 AM
Thanks Frenk,

I also thought of injecting XAML  into the code like you suggested, but since in my case only the bindings change I thought that it could be done  differently.

I was written this reply and I came up with an idea, that works but that also is kind of stupid.

In Xaml

<DataTemplate x:Key="stringEdit">
                <Grid>
                    <TextBox x:Name="testeTB" Loaded="testeTB_Loaded"></TextBox>
                </Grid>
 </DataTemplate>


In code behind:

I have an global variable
Binding bd;

I attach an event handler to the BeginningEdit event
tableEditorGrid.BeginningEdit += new EventHandler<GridViewBeginningEditRoutedEventArgs>(tableEditorGrid_BeginningEdit);

 void tableEditorGrid_BeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
        {
            bd = ((GridViewDataColumn)e.Cell.DataColumn).DataMemberBinding;
        }

and have
  private void testeTB_Loaded(object sender, RoutedEventArgs e)
        {
            TextBox tb = (TextBox)(sender);
            tb.SetBinding(TextBox.TextProperty,bd);
        }
So when the user access the cell to edit, the BeginningEdit event gets triggered and then I store the binding, after that the datatemplate textbox gets loaded so then I dynamicalliy assign the binding that was previously stored.
This also works but is not a clean soluction.
Does anybody have some other suggestion?
 

Tags
GridView
Asked by
Ivo
Top achievements
Rank 1
Answers by
BOARD LAB
Top achievements
Rank 1
Ivo
Top achievements
Rank 1
Share this question
or