Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4331622
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:08:13+00:00 2026-05-21T10:08:13+00:00

I try to do binding from DataTemplate to myGridViewColumn. I want to display custom

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:08:13+00:00Added an answer on May 21, 2026 at 10:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a custom binding and want to make it configurable over App.config. The
I am currently building a custom binding which gets HTTP requests from a different
I have a function like this for binding the primary key from the selected
I am writing a custom WCF Binding and I want to run some code
i'm using flash builder 4..i want to ask something..how to binding data to custom
This is what I have <GridViewColumn Header=Status > <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel Orientation=Horizontal> <Image Source={Binding
try: spam.foo except AttributeError: do_somthing() (Is it wise to check an attribute like that
I have the following DataTemplate: <DataTemplate DataType={x:Type Client:WorkItem}> <Grid> <Grid.RowDefinitions> <RowDefinition Height=Auto /> <RowDefinition
Is there a way to access My.Resources thru Xaml? Like this <Image Source={Binding MyImage,
In my WPF try to separate my classes logic from any interface related data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.