【问题标题】:WPF databinding to instances of an object in XAMLWPF 数据绑定到 XAML 中的对象实例
【发布时间】:2015-01-21 21:53:32
【问题描述】:

以数据绑定为例,我使用两种方式填充列表框:一种在代码隐藏中,另一种在 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" });
        }
    }
}

【问题讨论】:

    标签: c# wpf xaml observablecollection


    【解决方案1】:

    您实际上并没有使用绑定。请改用ItemsSource="{Binding Source={StaticResource myDataSource}}"。

    【讨论】:

    • 做了同样的事情:我想在列表框 lbUsers2 中看到“Natasha Doe”(不使用后面的代码:不是:lbUsers2.ItemsSource = customUsers;)
    【解决方案2】:

    经过几个小时的搜索,我找到了解决问题的方法(将其留给其他有类似启动问题的人): -首先:将绑定 source 设置为周围的 object(在此示例中:对象是 MainWindow => DataContext = this;) -Second :要使绑定到您的变量成为可能,请对它们进行properties。 -第三:如果你想访问更深层次的变量,也可以创建它们的属性并在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 2021-12-16
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2018-03-29
      相关资源
      最近更新 更多