Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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( );
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
166 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...