我是WPF的新手并且对数据绑定有疑问: 作为一个例子,我使用两种方式来填充列表框:一个在代码隐藏中,一个在Xaml中。 在代码隐藏中,将列表框绑定到对象实例(在此示例中为可观察集合' basicUsers')是相当容易的。 在Xaml中,我没有成功绑定到实例。似乎只能绑定到一个对象(基于可观察集合的自定义类)。可以将此对象实例中的数据用作XAML中的数据源吗?
XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<src:CustomTypeUsers x:Key="myDataSource"/>
</Window.Resources>
<Grid>
<ListBox Name="lbUsers" DisplayMemberPath="Name" Margin="10,10,70,141" Width="212"></ListBox>
<ListBox Name="lbUsers2" ItemsSource="{StaticResource myDataSource}" DisplayMemberPath="Name" Margin="10,149,70,0" />
</Grid>
代码隐藏:
using System.Windows;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private ObservableCollection<User> basicUsers = new ObservableCollection<User>();
private CustomTypeUsers customUsers = new CustomTypeUsers();
public Window1()
{
InitializeComponent();
basicUsers.Add(new User() { Name = "John Doe" });
basicUsers.Add(new User() { Name = "Jane Doe" });
customUsers.Add(new User() { Name = "Natasha Doe" });
customUsers.Add(new User() { Name = "Angelina Doe" });
lbUsers.ItemsSource = basicUsers;
}
private void btnAddUser_Click(object sender, RoutedEventArgs e)
{
basicUsers.Add(new User() { Name = "New user" });
}
}
public class User
{
public string Name { get; set; }
}
public class CustomTypeUsers : ObservableCollection<User>
{
public CustomTypeUsers()
{
Add(new User() { Name = "Emma Doe" });
Add(new User() { Name = "Betty Doe" });
}
}
}
答案 0 :(得分:2)
你实际上并没有使用Binding。请改为ItemsSource="{Binding Source={StaticResource myDataSource}}"
。
答案 1 :(得分:1)
经过几个小时的搜索,我找到了一个解决方案来解决我的问题(将其留给其他有类似启动问题的人): -First:将绑定源设置为周围的对象(在此示例中:对象为MainWindow =&gt; DataContext = this;) - 第二:为了使你的变量能够绑定,可以使它们成为属性。 - 第三:如果你想访问更深层次的变量,还要创建它们的属性并在XAML中访问它们,就像在c#中那样:例如:User。[1] .Name。
在代码中:
using System.Windows;
using System.Collections.ObjectModel;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public ObservableCollection<User> basicUsers { get; private set; }
public CustomTypeUsers customUsers { get; private set; }
public Window1()
{
InitializeComponent();
basicUsers = new ObservableCollection<User>();
basicUsers.Add(new User() { Name = "John Doe" });
basicUsers.Add(new User() { Name = "Jane Doe" });
customUsers = new CustomTypeUsers();
customUsers.Add(new User() { Name = "Natasha Doe" });
customUsers.Add(new User() { Name = "Angelina Doe" });
DataContext = this;
}
}
public class User
{
public string Name { get; set; }
}
public class CustomTypeUsers : ObservableCollection<User>
{
public CustomTypeUsers()
{
Add(new User() { Name = "John Doe" });
Add(new User() { Name = "Betty Doe" });
}
}
}
在XAML中:
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication2"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<!-- source=DataContext is already set in code behind to object where collections are defined = MainWindow = this -->
<!-- if defined in other classes use for example : <csrc:CustomTypeUsers x:Key="refSource"/> -->
</Window.Resources>
<Grid>
<ListBox Name="lbUsers" ItemsSource="{Binding basicUsers}" DisplayMemberPath="Name" Margin="10,10,70,141" Width="212"></ListBox>
<ListBox Name="lbUsers2" ItemsSource="{Binding customUsers}" DisplayMemberPath="Name" Margin="10,149,70,0" />
<TextBlock Text="{Binding Path=customUsers.[2].Name, Mode=OneWay}" Margin="196,128,0,116"/>
</Grid>