WPF databinding to an other class
Solution 1
You need to change a few things to make this work in your scenario:
Set the correct DataContext for your window:
public MainWindow() { InitializeComponent(); DataContext = new ServiceLogic(); }
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.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>
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.
Comments
-
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 about 11 yearsAllright, 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 about 11 yearsThat 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 about 11 yearssetting the window's data context in code can be messy as it can affect all bindings in that xaml