如何在代码中绑定到DataTemplate? [英] How can I bind to a DataTemplate in code?
问题描述
我有一个在XAML中定义的数据模板,可以通过调用 App.Current.Resources [ foo]
进行访问。我无法在 XAML
中定义模板上的数据绑定,因为无法从可视化树访问绑定的源(源为 DataGridColumn
类型对象),所以我必须在后面的代码中定义绑定。
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.
如何将绑定附加到 DataTemplate
,这样在调用 .LoadContent()
时,结果将尊重指定的数据绑定吗?
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:
确定已为APP.XAML文件设置了构建操作
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:
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( );
}
}
}
推荐答案
如何将绑定附加到
DataTemplate
,以便当.LoadContent()$ c $调用c>时,结果将尊重指定的数据绑定吗?
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.
而不是在 App.xaml
中定义 DataTemplate
,您可以考虑使用 XamlReader.Parse
方法以编程方式创建具有不同数据绑定的不同模板,例如:
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;
这篇关于如何在代码中绑定到DataTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!