Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

c# - How can I bind to a DataTemplate in code?

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

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

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...