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 581983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:38:17+00:00 2026-05-13T14:38:17+00:00

Since this is WPF, it may look like lots of code, but don’t be

  • 0

Since this is WPF, it may look like lots of code, but don’t be frightened, the question is really simple!

I have the following XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:hax="clr-namespace:hax" x:Class="hax.MainWindow"
    x:Name="Window" Title="Haxalot" Width="640" Height="280">

    <Grid x:Name="LayoutRoot">
        <ListView ItemsSource="{Binding AllRoles}" Name="Hello">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name"
                       DisplayMemberBinding="{Binding Path=FullName}"/>
                    <GridViewColumn Header="Role"
                       DisplayMemberBinding="{Binding Path=RoleDescription}"/>
                </GridView>
            </ListView.View>
        </ListView> 
    </Grid>
</Window>

I have this code-behind:

using System.Collections.ObjectModel;
using System.Windows;

namespace hax
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public ObservableCollection<Role> AllRoles { get { return m_AllRoles; } set { m_AllRoles = value; } }
        private ObservableCollection<Role> m_AllRoles = new ObservableCollection<Role>();

        public MainWindow()
        {
            this.InitializeComponent();

            AllRoles.Add(new Role("John", "Manager"));
            AllRoles.Add(new Role("Anne", "Trainee"));
            // Hello.ItemsSource = AllRoles; // NOTE THIS ONE!
        }
    }
}

If I leave the statement Hello.ItemSource = AllRoles commented out, the grid displays nothing. When I put it back in, it displays the correct thing. Why is this?

  • 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-13T14:38:17+00:00Added an answer on May 13, 2026 at 2:38 pm

    This:

    <ListView ItemsSource="{Binding AllRoles}" Name="Hello">
    

    means “Bind ItemsSource to the property this.DataContext.AllRoles” where this is the current element.

    Hello.ItemsSource = AllRoles;
    

    means “Bind ItemsSource to an ObservableCollection<T> full of roles”, which directly does what you were trying to do originally.

    There are a number of ways to do this in xaml. Here’s one:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            var allRoles = new ObservableCollection<Role>()
            allRoles.Add(new Role("John", "Manager"));
            allRoles.Add(new Role("Anne", "Trainee"));
            this.DataContext = allRoles;
        }
    }
    

    and in the xaml

    <ListView ItemsSource="{Binding}" Name="Hello">
    

    OR, alternatively, you could make AllRoles a public property of the window

    public partial class MainWindow : Window
    {
        public ObservableCollection<Role> AllRoles {get;private set;}
        public MainWindow()
        {
            this.InitializeComponent();
            var allRoles = new ObservableCollection<Role>()
            allRoles.Add(new Role("John", "Manager"));
            allRoles.Add(new Role("Anne", "Trainee"));
            this.AllRoles = allRoles;
        }
    }
    

    and then use a RelativeSource to tell the Binding to walk up the logical tree to the Window

    <ListView 
      ItemsSource="{Binding AllRoles, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" 
      Name="Hello">
    

    Which means “Look at my ancestry until you find a Window, then look for a public property on the window called AllRoles”.

    But the best way to do this is to skip the frigging codebehind altogether and use the MVVM pattern. I’d suggest if you’re learning that you skip directly to the MVVM pattern. The learning curve is steep, but you learn all about binding and commands and the important, cool things about WPF.

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

Sidebar

Related Questions

This may be a no-brainer for the WPF cognoscenti, but I'd like to know
Similar to this question, but for VB.NET since I learned this is a language
This might seem like a ridiculous question since I'm quite inexperienced in .NET. Is
I'm relatively new to WPF. I'm examining some code that looks like this: private
This may be a stupid question but I just can't find the answer. What
Question may need to be edited since I don't think I necessarily know the
This may be seen as a duplicate of Thread safety, lists, binding and WPF
i am writing a commercial WPF application that needs to be copy-protected. since this
In my WPF Window_Loaded event handler I have something like this: System.Threading.Tasks.Task.Factory.StartNew(() => {
I've created an add-in, which call via Reflection, a WPF Class Library. Since this

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.