-1

I have a data template defined in XAML and accessible by calling App.Current.Resources["foo"]. I can't define the Data Bindings on the template in XAML because the source for the bindings is not accessible from the visual tree ( the sources are DataGridColumn type objects ), so I have to define the bindings in code behind.

How do I attach bindings to a DataTemplate so that when .LoadContent( ) is called, the result will have respect the specified data bindings?

In compliance with the Minimal, Complete and Verifiable Example Requirements:

MAKE CERTAIN THAT THE BUILD ACTION FOR THE APP.XAML FILE IS SET TO PAGE.

App.xaml:

<Application
    x:Class="MCVE.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MCVE" StartupUri="MainWindow.xaml">
    <Application.Resources>
        <DataTemplate x:Key="Template">
            <TextBlock />
        </DataTemplate>
    </Application.Resources>
</Application>

App.xaml.cs:

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using System.Xml;

namespace MCVE {
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
    public partial class App {
        [STAThread]
        public static int Main( ) {
            App program = new App( );
            program.InitializeComponent( );
            DataTemplate foo = program.Resources["Template"] as DataTemplate;
            DataGridTemplateColumn source = new DataGridTemplateColumn( );
            source.Header = "Foo";
            Binding b = new Binding( ) {
                Path = new PropertyPath(
                    DataGridColumn.HeaderProperty ),
                Source = source
            };

            //How do I set the binding to the TextBlock within
            //the DataTemplate so that the next time the
            //template is loaded, the TextBlock will
            //read "Foo"?

            return program.Run( );
        }
    }
}
Will
  • 3,413
  • 7
  • 50
  • 107
  • I don't unserstand why DataGridTemplateColumn has to be the Source for binding. binding Source is some meaningful data used in business logic. strange to seeDataGridTemplateColumn without DataGrid, and DataGrid assigns DataContext (and therefore Binding source) to cells automatically – ASh Apr 24 '18 at 07:14

2 Answers2

0

is this what you need!

take your xaml code in Page and build.

 <DataGrid Name="Users" AutoGenerateColumns="False">


     <DataGrid Name="dgUsers" AutoGenerateColumns="False">
                            <DataGrid.Columns>

                                    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />

                                    <DataGridTemplateColumn Header="Birthday">
                                            <DataGridTemplateColumn.CellTemplate>
                                                    <DataTemplate>
                                                            <DatePicker SelectedDate="{Binding Birthday}" BorderThickness="0" />
                                                    </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>

                            </DataGrid.Columns>
                    </DataGrid>
Alex Dave
  • 108
  • 12
0

How do I attach bindings to a DataTemplate so that when .LoadContent( ) is called, the result will have respect the specified data bindings?

You define the bindings in the template itself. You cannot define a template witout bindings and then apply the bindings to this template on the fly. A template must de defined "as a whole" so this approach won't work I am afraid.

Instead of defining the DataTemplate in App.xaml, you may consider to create different templates with different data bindings programmatically using the XamlReader.Parse method, e.g.:

string fooTemplate = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x = \"http://schemas.microsoft.com/winfx/2006/xaml\"><TextBlock Text=\"{Binding Foo}\"></DataTemplate>";
source.CellTemplate = XamlReader.Parse(fooTemplate) as DataTemplate;
mm8
  • 163,881
  • 10
  • 57
  • 88