WPF databinding to an other class

15,104

Solution 1

You need to change a few things to make this work in your scenario:

  1. Set the correct DataContext for your window:

    public MainWindow()
    {   
        InitializeComponent();
        DataContext = new ServiceLogic();
    }
    
  2. Make sure that ServiceLogic has a public property named Users:

    public List<User> Users { get; set; }
    

    if you want to add/remove items to this List at runtime, consider using an ObservableCollection<T> as this will notify the UI of any changes automatically.

  3. Update the binding logic of your xaml, so that you bind to the correct list. Also set the DisplayMemberPath property or add a template so that the objects are displayed nicely:

    <ListBox ItemsSource="{Binding Path=Users}" DisplayMemberPath="Name"/>
    

    or

    <ListBox ItemsSource="{Binding Path=Users}">
    <ListBox.ItemTemplate>
         <DataTemplate>
                <...your data template, like grid or stackpanel/>
         </DataTemplate>
    </ListBox.DataTemplate>
    

  4. When using DisplayMemberPath, make sure the User-class has the correct properties. Add the following to User.cs:

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    } 
    

Solution 2

Here ItemsSource="{Binding Path=ServiceLogic.Users}" you state that data has public property ServiceLogic

Second, you data is acquired through DataContext

Change constructor:

public MainWindow()
{
    InitializeComponent();
    serviceLogic = new ServiceLogic ();
    DataContext = serviceLogic;
}

and change binding to this one:

<ListBox Height="100" HorizontalAlignment="Left" Margin="6,44,0,0" 
 Name="listBox_detected" VerticalAlignment="Top" Width="120" 
 ItemsSource="{Binding Path=Users}" />

In Binding I removed ServiceLogic because SL stands as data item. And Path - is the path of the property.

Solution 3

I think you need to set "DisplayMemberPath" property of ListBox.

Share:
15,104
Joetjah
Author by

Joetjah

SOreadytohelp #SOreadytohelp

Updated on June 14, 2022

Comments

  • Joetjah
    Joetjah almost 2 years

    I've created a WPF UI. The following code exists in MainWindow.xaml.cs:

    namespace AWPFProject
    {
        public partial class MainWindow : Window
        {
            private readonly ServiceLogic serviceLogic;
    
            public MainWindow()
            {
                InitializeComponent();
                serviceLogic = new ServiceLogic ();
            }
        }
    }
    

    Servicelogic is my central class. From there, methods or classes are called to handle stuff like database management.

    Now, that ServiceLogic class has the values I'd like to bind to. For example, I have a combobox where I can show my users. The XAML looks like this:

    <ListBox Height="100" HorizontalAlignment="Left" Margin="6,44,0,0" 
     Name="listBox_detected" VerticalAlignment="Top" Width="120" 
     ItemsSource="{Binding Path=ServiceLogic.Users}" />
    

    When I run the application, the list remains emtpy. What else do I need to do to get that information in my list?

  • Joetjah
    Joetjah about 11 years
    Allright, it's that easy. The next problem is that my list is showing the type (Entities.User) instead of the name of the user, but I guess this question is solved!
  • Joetjah
    Joetjah about 11 years
    That DisplayMemberPath solved my next problem! But it didn't work without adding an extra method to the User-class. Do you mind if I add that to your answer to make it more complete?
  • Sten Petrov
    Sten Petrov about 11 years
    setting the window's data context in code can be messy as it can affect all bindings in that xaml