How can I programmatically create a WPF Toolkit DataGridTemplateColumn?

I managed to recreate this XAML DataGridTextColumn :

<tk:DataGridTextColumn
    Binding="{Binding FirstName}"
    Header="First Name"/>

in code :

DataGridTextColumn dgtc = new DataGridTextColumn();
dgtc.Header = propertyLabel;
dgtc.Binding = new Binding(propertyName);
theDataGrid.Columns.Add(dgtc);

But how can I recreate the following DataGridTemplateColumn code in the code?

<tk:DataGridTemplateColumn Width="100">
    <tk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Style="{DynamicResource ManageLinkStyle}"
                    Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
                <TextBlock Text=" "/>
                <TextBlock Style="{DynamicResource ManageLinkStyle}"
                           Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
            </StackPanel>
        </DataTemplate>
    </tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>

i.e. I am fixated on the definition of CellTemplate:

DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.CellTemplate = new CellTemplate ...???

Answer:

Thanks, Aran, simply referring to the template key in XAML, works well for what I need, here is how I changed above to work for me:

XAML:

<Window.Resources>
    <DataTemplate x:Key="manageAreaCellTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Style="{DynamicResource ManageLinkStyle}"
        Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
            <TextBlock Text=" "/>
            <TextBlock Style="{DynamicResource ManageLinkStyle}"
               Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

background code:

DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
dgTemplateColumn.Header = "Manage Options";
dgTemplateColumn.CellTemplate = this.FindResource("manageAreaCellTemplate") as DataTemplate;
theDataGrid.Columns.Add(dgTemplateColumn);
+3
source share
3 answers

would it be acceptable for you to define a cell template in xaml yet?

then you can define it

<DataTemplate x:Key="myCellTemplateKey">
    <StackPanel Orientation="Horizontal">
        <TextBlock Style="{DynamicResource ManageLinkStyle}"
            Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
        <TextBlock Text=" "/>
        <TextBlock Style="{DynamicResource ManageLinkStyle}"
                   Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
    </StackPanel>
</DataTemplate>

, xaml.

dgTemplateColumn.CellTemplate = this.FindResource("myCellTemplateKey") as DataTemplate;

FrameworkElementFactory , , - , , ( , , ). , , , . . .

+4

:

DataGridTemplateColumn col1 = new DataGridTemplateColumn();
col1.Header = "MyHeader";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(CheckBox));
Binding b1 = new Binding("IsSelected");
b1.Mode = BindingMode.TwoWay;
factory1.SetValue(CheckBox.IsCheckedProperty, b1);
factory1.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(chkSelect_Checked));
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
dgTransportReqsts.DataGrid.Columns.Add(col1);

, CheckBox DataGridTemplateColumn . , !

+4

FrameworkElementFactory .

private DataTemplate CreateCellTemplate()
{
   string xaml = @"
   <DataTemplate
   xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
  <StackPanel>
      <TextBlock Text='HelloTestmaster'/>
   </StackPanel>
    </DataTemplate>
   ";
   StringReader stringReader = new StringReader(xaml);
   XmlReader xmlReader = XmlReader.Create(stringReader);
   return (DataTemplate)XamlReader.Load(xmlReader);
}
+1

Source: https://habr.com/ru/post/1723250/


All Articles