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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:14:00+00:00 2026-05-27T05:14:00+00:00

I’m a (relatively) experienced Cocoa/Objective-C coder, and am teaching myself C# and the WPF

  • 0

I’m a (relatively) experienced Cocoa/Objective-C coder, and am teaching myself C# and the WPF framework.

In Cocoa, when populating an NSTableView, it’s relatively simply to assign a delegate and datasource to the view. Those delegate/datasource methods are then used to populate the table, and to determine its behavior.

I’m putting together a simple application that has a list of objects, lets call them Dog objects, that each have a public string name. This is the return value of Dog.ToString().

The objects will be displayed in a ListBox, and I would like to populate this view using a similar pattern to Cocoa’s NSTableViewDataSource. It currently seems to be working using:

public partial class MainWindow : Window, IEnumerable<Dog>
    {
        public Pound pound = new Pound();

        public MainWindow()
        {
            InitializeComponent();

            Dog fido = new Dog();
            fido.name = "Fido";
            pound.AddDog(fido);

            listBox1.ItemsSource = this;

            Dog spot = new Dog();
            spot.name = "Spot";
            pound.AddDog(spot);
        }

        public IEnumerator<Dog> GetEnumerator()
        {
            return currentContext.subjects.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

But I’m wondering how correct this is. I’ve literally had Visual Studio installed for less than an hour, so it’s safe to say I have no idea what I’m doing.

  1. Is this the proper pattern?
  2. Adding the second item to the list (spot) seems to update the ListBox properly, but I’m wondering what triggers the updates?
  3. What happens if I update the Pound on a background thread?
  4. How can I manually ask the ListBox to update itself? (Do I even need to?)

One change that I know I need to make is refactoring the IEnumerable<Dog> implementation into its own class, like DogListItemsSource, but I want to make sure I have a solid approach before polishing it.

Feel free to point out, in comments, any other points I should address or keep in mind, big or small. I’d like to learn this the right way, the first time.

  • 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-27T05:14:01+00:00Added an answer on May 27, 2026 at 5:14 am

    My suggestion would be to create a class besides your Window which would be responsible for providing the data to your ListBox. A common approach is WPF is called MVVM, which like any pattern has many implementations.

    The basics are each Model (e.g. Pound and Dog) would have a View Model responsible for presenting the model in a manner which is easy to interact with from the UI.

    To get you started, WPF provides an excellent class, ObservableCollection<T>, which is a collection that fires off a “Hey I Changed” event whenever anybody is added, moved, or removed.

    Below is an example that doesn’t intend to teach you MVVM, nor does it use any framework for MVVM. However, if you set some breakpoints and play with it, you’ll learn about bindings, commands, INotifyPropertyChanged, and ObservableCollection; all of which play a large role in WPF application development.

    Starting in the MainWindow, you can set your DataContext to a View Model:

    public class MainWindow : Window
    {
         // ...
         public MainWindow()
         {
             // Assigning to the DataContext is important
             // as all of the UIElement bindings inside the UI
             // will be a part of this hierarchy
             this.DataContext = new PoundViewModel();
    
             this.InitializeComponent();
         }
    }
    

    Where the PoundViewModel manages a collection of DogViewModel objects:

    public class PoundViewModel
    {
        // No WPF application is complete without at least 1 ObservableCollection
        public ObservableCollection<DogViewModel> Dogs
        {
            get;
            private set;
        }
    
        // Commands play a large role in WPF as a means of 
        // transmitting "actions" from UI elements
        public ICommand AddDogCommand
        {
            get;
            private set;
        }
    
        public PoundViewModel()
        {
            this.Dogs = new ObservableCollection<DogViewModel>();
    
            // The Command takes a string parameter which will be provided
            // by the UI. The first method is what happens when the command
            // is executed. The second method is what is queried to find out
            // if the command should be executed
            this.AddDogCommand = new DelegateCommand<string>(
                name => this.Dogs.Add(new DogViewModel { Name = name }),
                name => !String.IsNullOrWhitespace(name)
            );
        }
    }
    

    And in your XAML (be sure to map xmlns:local to allow XAML to use your View Models):

    <!-- <Window ...
                 xmlns:local="clr-namespace:YourNameSpace" -->
    <!-- Binding the ItemsSource to Dogs, will use the Dogs property
      -- On your DataContext, which is currently a PoundViewModel
      -->
    <ListBox x:Name="listBox1"
             ItemsSource="{Binding Dogs}">
        <ListBox.Resources>
            <DataTemplate DataType="{x:Type local:DogViewModel}">
                <Border BorderBrush="Black" BorderThickness="1" CornerRadius="5">
                    <TextBox Text="{Binding Name}" />
                </Border>
            </DataTemplate>
        </ListBox.Resources>
    </ListBox>
    <GroupBox Header="New Dog">
        <StackPanel>
            <Label>Name:</Label>
            <TextBox x:Name="NewDog" />
    
            <!-- Commands are another big part of WPF -->
            <Button Content="Add"
                    Command="{Binding AddDogCommand}"
                    CommandParameter="{Binding Text, ElementName=NewDog}" />
        </StackPanel>
    </GroupBox>
    

    Of course, you’d need a DogViewModel:

    public class DogViewModel : INotifyPropertyChanged
    {
        private string name;
        public string Name
        {
            get { return this.name; }
            set
            {
                this.name = value;
    
                // Needed to alert WPF to a change in the data
                // which will then update the UI
                this.RaisePropertyChanged("Name");
            }
        }
    
        public event PropertyChangedHandler PropertyChanged;
    
        private void RaisePropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    Finally you’ll need an implementation of DelegateCommand<T>:

    public class DelegateCommand<T> : ICommand
    {
        private readonly Action<T> execute;
        private readonly Func<T, bool> canExecute;
        public event EventHandler CanExecuteChanged;
    
        public DelegateCommand(Action<T> execute, Func<T, bool> canExecute)
        {
            if (execute == null) throw new ArgumentNullException("execute");
            this.execute = execute;
            this.canExecute = canExecute;
        }
    
        public bool CanExecute(T parameter)
        {
            return this.canExecute != null && this.canExecute(parameter); 
        }
    
        bool ICommand.CanExecute(object parameter)
        {
            return this.CanExecute((T)parameter);
        }
    
        public void Execute(T parameter)
        {
            this.execute(parameter);
        }
    
        bool ICommand.Execute(object parameter)
        {
            return this.Execute((T)parameter);
        }
    }
    

    This answer by no means will have you whipping up immersive, fully bound WPF UI’s, but hopefully it’ll give you a feel for how the UI can interact with your code!

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.