How can I bind to a DataTemplate in code?

余生长醉 提交于 2019-12-14 04:12:14

问题


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( );
        }
    }
}

回答1:


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>



回答2:


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;


来源:https://stackoverflow.com/questions/49991828/how-can-i-bind-to-a-datatemplate-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!