WPF 数据绑定到另一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14982786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF databinding to an other class
提问by Joetjah
I've created a WPF UI. The following code exists in MainWindow.xaml.cs:
我创建了一个 WPF UI。以下代码存在于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.
Servicelogic 是我的中心课程。从那里,调用方法或类来处理诸如数据库管理之类的事情。
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:
现在,该 ServiceLogic 类具有我想要绑定到的值。例如,我有一个组合框,可以在其中显示我的用户。XAML 看起来像这样:
<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?
当我运行应用程序时,列表仍然是空的。我还需要做什么才能在我的列表中获取该信息?
回答by RoelF
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
ServiceLogichas a public propertynamed 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
DisplayMemberPathproperty 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; } }
为您的窗口设置正确的 DataContext:
public MainWindow() { InitializeComponent(); DataContext = new ServiceLogic(); }确保
ServiceLogic有一个名为 Users的公共属性:public List<User> Users { get; set; }如果您想在运行时向此列表添加/删除项目,请考虑使用 ,
ObservableCollection<T>因为这将自动通知 UI 任何更改。更新您的 xaml 的绑定逻辑,以便您绑定到正确的列表。还要设置
DisplayMemberPath属性或添加模板,以便很好地显示对象:<ListBox ItemsSource="{Binding Path=Users}" DisplayMemberPath="Name"/>或者
<ListBox ItemsSource="{Binding Path=Users}"> <ListBox.ItemTemplate> <DataTemplate> <...your data template, like grid or stackpanel/> </DataTemplate> </ListBox.DataTemplate>使用时
DisplayMemberPath,请确保用户类具有正确的属性。将以下内容添加到 User.cs:public string Name { get { return _name; } set { _name = value; } }
回答by Pavel Voronin
Here ItemsSource="{Binding Path=ServiceLogic.Users}"you state that data has public propertyServiceLogic
在这里ItemsSource="{Binding Path=ServiceLogic.Users}"您声明数据具有公共财产ServiceLogic
Second, you data is acquired through DataContext
其次,您的数据是通过 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.
在绑定中,我删除了 ServiceLogic,因为 SL 代表数据项。Path - 是属性的路径。
回答by Mukesh Rawat
I think you need to set "DisplayMemberPath" property of ListBox.
我认为您需要设置 ListBox 的“DisplayMemberPath”属性。

