Search code examples
c#wpfgridviewbindingdatatemplate

Binding DataTemplate content


I have the following DataTemplate:

<DataTemplate x:Key="ButtonTemplate">
    <Button Click="Cell_Click">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <TextBlock x:Name="TBlock" />
            </ControlTemplate>
        </Button.Template>
    </Button>
</DataTemplate>

And I use this DataTemplate as GridViewColumn.CellTemplate, but I have to bind dynamically the Text Property of the TextBlock when I add a new column:

GridViewColumn column = new GridViewColumn();
column.CellTemplate = Resources["ButtonTemplate"] as DataTemplate;
// How to get the TBlock (TextBlock) of CellTemplate and bind its property here?
MyGridView.Columns.Add(column);

What have I to do? Thanks.


Solution

  • Forget about modifying the template, you better create it from scratch in code (or you can use XamlReader.Parse and a XAML string with the binding code inserted dynamically) .