从 DataTemplate 到 GridViewColumn 的 WPF 绑定

发布于 2024-10-31 10:07:50 字数 4078 浏览 1 评论 0原文

我尝试从 DataTemplate 绑定到 myGridViewColumn。我想在网格视图标题中显示自定义文本(例如“Caption =“Name””),但它不起作用!

XAML DataTemplate:

<Window x:Class="DataTemplateTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:DataTemplateTest" Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="System3" DataType="{x:Type my:MyGridViewColumn}">
            <StackPanel Grid.Column="0" Margin="2"  Orientation="Horizontal">
                <TextBlock  Text="113 " Foreground="Red"/>
                <TextBlock  Text="{Binding Path=Caption}"/>
                <TextBlock  Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:MyGridViewColumn}}, Path=Caption}"/>
                <TextBlock  Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Caption}"/>
            </StackPanel>
        </DataTemplate>       
    </Window.Resources>
    <Grid>
        <ListView
            Height="auto"
            SelectionMode="Single"
            Name="lstvMain"
            >
            <ListView.View>
                <GridView>
                    <my:MyGridViewColumn HeaderTemplate="{StaticResource ResourceKey=System3}" Width="150" Caption="Name" DisplayMemberBinding="{Binding Path=Name}"/>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                    <GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Path=Surname, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

和 C# 代码

#region code
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace DataTemplateTest
{
    public partial class MainWindow
    {
        public List<User> Users = new List<User> { new User { Name = "John", Surname = "Smith" }, new User { Name = "Joe", Surname = "Brown" } };

        public MainWindow()
        {
            InitializeComponent();
            lstvMain.ItemsSource = Users;
        }
    }
    public class User : DependencyObject
    {
        public static DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(String), typeof(User), new PropertyMetadata(String.Empty));
        public String Name
        {
            get { return (String)GetValue(NameProperty); }
            set
            {
                SetValue(NameProperty, value);
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(NameProperty, null, value));
            }
        }

        public static DependencyProperty SurnameProperty = DependencyProperty.Register("Surname", typeof(String), typeof(User), new PropertyMetadata(String.Empty));
        public String Surname
        {
            get { return (String)GetValue(SurnameProperty); }
            set
            {
                SetValue(SurnameProperty, value);
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(SurnameProperty, null, value));
            }
        }
    }
    public class MyGridViewColumn : GridViewColumn
    {
        public static DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(String), typeof(MyGridViewColumn), new PropertyMetadata(String.Empty));
        public String Caption
        {
            get { return (String)GetValue(CaptionProperty); }
            set
            {
                SetValue(CaptionProperty, value);
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(CaptionProperty, null, value));
            }
        }
    }
}
#endregion

如有任何帮助,我们将不胜感激!

I try to do binding from DataTemplate to myGridViewColumn. I want to display custom text (like 'Caption="Name"') in grid view header, but it doesn't work!

XAML DataTemplate:

<Window x:Class="DataTemplateTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:DataTemplateTest" Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="System3" DataType="{x:Type my:MyGridViewColumn}">
            <StackPanel Grid.Column="0" Margin="2"  Orientation="Horizontal">
                <TextBlock  Text="113 " Foreground="Red"/>
                <TextBlock  Text="{Binding Path=Caption}"/>
                <TextBlock  Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:MyGridViewColumn}}, Path=Caption}"/>
                <TextBlock  Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Caption}"/>
            </StackPanel>
        </DataTemplate>       
    </Window.Resources>
    <Grid>
        <ListView
            Height="auto"
            SelectionMode="Single"
            Name="lstvMain"
            >
            <ListView.View>
                <GridView>
                    <my:MyGridViewColumn HeaderTemplate="{StaticResource ResourceKey=System3}" Width="150" Caption="Name" DisplayMemberBinding="{Binding Path=Name}"/>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                    <GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Path=Surname, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

and C# code

#region code
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace DataTemplateTest
{
    public partial class MainWindow
    {
        public List<User> Users = new List<User> { new User { Name = "John", Surname = "Smith" }, new User { Name = "Joe", Surname = "Brown" } };

        public MainWindow()
        {
            InitializeComponent();
            lstvMain.ItemsSource = Users;
        }
    }
    public class User : DependencyObject
    {
        public static DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(String), typeof(User), new PropertyMetadata(String.Empty));
        public String Name
        {
            get { return (String)GetValue(NameProperty); }
            set
            {
                SetValue(NameProperty, value);
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(NameProperty, null, value));
            }
        }

        public static DependencyProperty SurnameProperty = DependencyProperty.Register("Surname", typeof(String), typeof(User), new PropertyMetadata(String.Empty));
        public String Surname
        {
            get { return (String)GetValue(SurnameProperty); }
            set
            {
                SetValue(SurnameProperty, value);
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(SurnameProperty, null, value));
            }
        }
    }
    public class MyGridViewColumn : GridViewColumn
    {
        public static DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(String), typeof(MyGridViewColumn), new PropertyMetadata(String.Empty));
        public String Caption
        {
            get { return (String)GetValue(CaptionProperty); }
            set
            {
                SetValue(CaptionProperty, value);
                OnPropertyChanged(new DependencyPropertyChangedEventArgs(CaptionProperty, null, value));
            }
        }
    }
}
#endregion

Any help will be appreciated!

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

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

发布评论

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

评论(1

金兰素衣 2024-11-07 10:07:50

您定义了 HeaderTemplate,但没有 Header,因此模板没有 DataContext

但无论如何,您将无法直接定义标头使用绑定,因为 GridViewColumn 不继承 DataContext。我在博客中介绍了此问题的解决方案 此处

You defined a HeaderTemplate, but no Header, so the template has no DataContext

But anyway, you won't be able to define the header directly with a binding, because the GridViewColumn doesn't inherit the DataContext. I blogged about a solution to this problem here.

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