簡體   English   中英

WPF數據綁定到XAML中的對象的實例

[英]WPF databinding to instances of an object in XAML

以數據綁定為例,我使用兩種方式填充列表框:一種在代碼隱藏中,一種在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" });
        }
    }
}

您實際上並沒有使用綁定。 請嘗試使用ItemsSource="{Binding Source={StaticResource myDataSource}}"

經過幾個小時的搜索,我找到了一個解決問題的方法(將其留給有類似啟動問題的其他人使用):-首先:將綁定設置為周圍的對象 (在本示例中:該對象為MainWindow => DataContext = this ;)-第二:使綁定到變量成為可能,並對其進行屬性設置。 -Third:如果要訪問更深層的變量,則還應使其屬性並在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM