WPF 模板和绑定到 GridView 中的 DataContext

发布于 2024-08-23 12:57:52 字数 964 浏览 8 评论 0原文

我正在尝试在 RadGridView 中创建一系列绑定列,并使用模板在其中两列中创建超链接。这基本上是我所拥有的:

<telerik:GridViewDataColumn IsReadOnly="True" UniqueName="Distributor" DataContext="{Binding Distributor}" CellTemplate="{StaticResource linkTemplate}"/>

并且,

    <DataTemplate x:Key="linkTemplate">
        <TextBlock>
            <Hyperlink DataContext={TemplateBinding DataContext} Click="Hyperlink_Click">
                <TextBlock Text="{Binding Name}" />
            </Hyperlink>
        </TextBlock>
    </DataTemplate>

RadGridView 本身绑定到一组 DistributorContainer 对象,这些对象除其他外还具有 Distributor 属性。 linkTemplate 直接引用Distributor 对象中的属性,因此需要将超链接的数据上下文设置为Distributor。

不幸的是,超链接的数据上下文是DistributorContainer 对象。我在绑定到分销商列表的列表上使用 linkTemplate(以及 Hyperlink_Click 处理程序),并且我真的很想重新使用此模板,因为它基本上是相同的东西。

为什么模板没有通过到 GridViewDataColumn 的 TemplateBinding 获取 Distributor 作为其 DataContext?

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. 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. The linkTemplate refers directly to properties in the Distributor object, so the hyperlink's datacontext needs to be set to the Distributor.

Unfortunately, the Hyperlink's data context is the DistributorContainer object. 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.

Why isn't the template getting the Distributor as its DataContext through the TemplateBinding to the GridViewDataColumn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2024-08-30 12:57:52

以下是如何实现此目的的示例:

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#

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

Here is an example how to achieve this:

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#

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; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文