2

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
  • 6,292
  • 8
  • 55
  • 90
  • make ServiceLogic a read-only dependency property of MainWindow, see http://stackoverflow.com/questions/1122595/how-do-you-create-a-read-only-dependency-property – Sten Petrov Feb 20 '13 at 14:54
  • I'm afraid the answer given there makes absolutely no sense to me... – Joetjah Feb 20 '13 at 15:04
  • Should I manually register all the ServiceLogic's variables? I've got a lot of variables there.... – Joetjah Feb 20 '13 at 15:10
  • Right above the `public MainWindow()` copy the answer linked above and rename every relevant piece. If that doesn't make sense look up "Dependency properties WPF" in google. You don't have to map ServiceLogic's properties but at the very least you should implement INotifyPropertyChanged for them so that when they are updated the UI picks that up. – Sten Petrov Feb 20 '13 at 15:38
  • Roel's answer down here made everything clear! Thank you anyway for the effort :) – Joetjah Feb 20 '13 at 15:53

3 Answers3

7

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; }
    } 
    
hb.
  • 1,705
  • 5
  • 22
  • 43
RoelF
  • 7,483
  • 5
  • 44
  • 67
  • 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? – Joetjah Feb 20 '13 at 15:29
1

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.

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
  • 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 Feb 20 '13 at 15:25
  • setting the window's data context in code can be messy as it can affect all bindings in that xaml – Sten Petrov Feb 20 '13 at 15:41
1

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

Mukesh Rawat
  • 2,047
  • 16
  • 30