[英]WPF Templates and binding to DataContext in a GridView
I'm trying to create a series of bound columns in a RadGridView, and I'm using a template to create hyperlinks in two of the columns. 我正在尝试在RadGridView中创建一系列绑定列,我正在使用模板在两个列中创建超链接。 Here is basically what I have:
这基本上就是我所拥有的:
<telerik:GridViewDataColumn IsReadOnly="True" UniqueName="Distributor" DataContext="{Binding Distributor}" CellTemplate="{StaticResource linkTemplate}"/>
and, 和,
<DataTemplate x:Key="linkTemplate">
<TextBlock>
<Hyperlink DataContext={TemplateBinding DataContext} Click="Hyperlink_Click">
<TextBlock Text="{Binding Name}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
The RadGridView itself is bound to a set of DistributorContainer objects that have, among other things, a Distributor property. RadGridView本身绑定到一组DistributorContainer对象,这些对象具有Distributor属性等。 The linkTemplate refers directly to properties in the Distributor object, so the hyperlink's datacontext needs to be set to the Distributor.
linkTemplate直接引用Distributor对象中的属性,因此需要将超链接的datacontext设置为Distributor。
Unfortunately, the Hyperlink's data context is the DistributorContainer object. 不幸的是,Hyperlink的数据上下文是DistributorContainer对象。 I'm using the linkTemplate (as well as the Hyperlink_Click handler) on lists that bind to lists of Distributors, and I'd really like to re-use this template since it's basically the same thing.
我在绑定到分销商列表的列表上使用linkTemplate(以及Hyperlink_Click处理程序),我真的想重新使用这个模板,因为它基本上是一样的。
Why isn't the template getting the Distributor as its DataContext through the TemplateBinding to the GridViewDataColumn? 为什么模板不通过TemplateBinding到GridViewDataColumn将Distributor作为其DataContext?
Here is an example how to achieve this: 以下是如何实现此目的的示例:
XAML XAML
<Grid>
<Grid.Resources>
<DataTemplate x:Key="linkTemplate">
<TextBlock>
<Hyperlink>
<TextBlock
Text="{Binding
Value.Name,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type telerik:GridViewCell}}}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</Grid.Resources>
<telerik:RadGridView ItemsSource="{Binding}" AutoGenerateColumns="False">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn
DataMemberBinding="{Binding Distributor1}"
CellTemplate="{StaticResource linkTemplate}" />
<telerik:GridViewDataColumn
DataMemberBinding="{Binding Distributor2}"
CellTemplate="{StaticResource linkTemplate}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
C# C#
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext =
from i in Enumerable.Range(0, 10)
select new DistributorContainer()
{
ID = i,
Distributor1 = new Distributor() {
Name = String.Format("Distributor1 Name{0}", i) },
Distributor2 = new Distributor() {
Name = String.Format("Distributor2 Name{0}", i) }
};
}
}
public class DistributorContainer
{
public int ID { get; set; }
public Distributor Distributor1 { get; set; }
public Distributor Distributor2 { get; set; }
}
public class Distributor
{
public string Name { get; set; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.