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.
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; }
That worked - many thanks Darrell!
What happens in case of Unbound columns?
How can I access the Unboundcell and change the control(Button) inside it to enable/disable?
I cannot do this at design time..have to decide it dynamically.Cannot use converter as Unbound columns are not bound to the datasource.
Thanks
Hi,
Sorry if i was unclear in my previous post, but with the UnboundColumn, the DataContext is different from the DataContext of a TemplateColumn. Instead, we offer more properties so users can have more options of what to bind to.
So, you can't binding directly to your data without specifying a Path anymore. Instead, if you want to bind directly to your data, you need
{Binding RowData} instead of {Binding}
and if you want to bind to a specific property in your data, you need:
{Binding RowData.PropertyName} instead of {Binding PropertyName}
Hope this helps clarify things.
-SteveZ
I am trying to use a converter in Unbound column ,specifying the data context property and using converter.the data template is as follows
<DataTemplate x:Key="deleteButtonColumnTemplate"> <Grid> <Button Tag="{Binding}" DataContext="{Binding}" Content="Delete" Click="deleteButton_Click" x:Name="deleteButton" IsEnabled="{Binding data_source , Converter=disableDeleteButton}"/> </Grid>
Note:data_source is a column in Grid and is also present in datasource object.
The column does not call the converter even after using DataContext.
I cannot access the unbound cel-button in events RowInitilize and ControlAttached.The cell's Control.Content property returns null in these events
The UnboundColumn's DataContext is of type UnboundColumnDataContext which contains the following properties:RowData, Value and ColumnKey.
So you definitely should still be able to user a converter as you have the data of the row in which the cell belongs to.