Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
415
Populating a TemplateColumn value at runtime
posted

I've added a TemplateColumn to my grid row. The value of this cannot be bound as it needs to be evaluated at runtime.

I've added an event handler to the InitializeRow event of the grid and I've got the cell object using .Row.Cells("dynamicDescription"). Is there any way I can access the TextBlock control within the template column and set it's value?

Many thanks.

  • 21382
    Verified Answer
    posted

    You probably want to use a  TypeConverter on the template column to provide your ItemTemplate with the correct value.

     

    Something similar to this:

     <UserControl.Resources>

                <local:TextConverter x:Key="cnvt"></local:TextConverter>

    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">

                <ig:XamWebGrid x:Name="dataGrid">

                      <ig:XamWebGrid.Columns>

                            <ig:TemplateColumn Key="CalcField">

                                  <ig:TemplateColumn.ItemTemplate>

                                        <DataTemplate>

                                              <TextBlock Text="{Binding Converter={StaticResource cnvt}}"></TextBlock>

                                        </DataTemplate>

                                  </ig:TemplateColumn.ItemTemplate>

                            </ig:TemplateColumn>

                      </ig:XamWebGrid.Columns>

                </ig:XamWebGrid>

          </Grid>

     

    And in the code behind:

    public class TextConverter : IValueConverter

    {

     

    #region IValueConverter Members

     

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

    MyObj obj = value as MyObj;

    return obj.Op1 + obj.Op2;

    }

     

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

    throw new NotImplementedException();

    }

     

    #endregion

    }

     

    public class MyObj

    {

    public int Op1 { get; set; }

    public int Op2 { get; set; }

    }