C# WPF 数据绑定和数据上下文

C# WPF Databinding and Datacontext

我(C# 和 WPF 的新手)正在尝试从更多来源(class 属性)绑定数据,但我对不同的指南和建议感到有点困惑。我想在 userList 中动态添加 Users 并同时显示最后一个插入和整个列表。这是在源代码的不同位置完成的,但很简单,就像在我的示例的构造函数中一样。我应该如何以及在何处为这三个元素(myNamemyTitlemyUserList)设置绑定和数据上下文以反映主要 class 属性的变化?我应该每次调用更新绑定目标的函数,还是在编辑属性后设置 this.datacontext?或者我应该绑定到某个 returns 我需要的值的函数(如果可能的话)?我对绑定到对象和数据上下文等的 属性 有点迷茫。这是我所拥有的示例:

<Window x:Name="Window" x:Class="WpfTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfTest">
  <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="200"/>
    </Grid.ColumnDefinitions>
    <StackPanel>
      <TextBox x:Name="myName" Text=""  Grid.Column="1"/>
      <TextBox x:Name="myTitle" Text=""  Grid.Column="0"/>
    </StackPanel>
    <ListBox x:Name="myUserList">
    </ListBox>
  </Grid>
</Window>

public partial class MainWindow : Window {
  public User myUser;
  public Dictionary<int,User> userList= new Dictionary<int, User>();

  public object SubWindow { get; private set; }

  public MainWindow() {
    newUser = new User();
    newUser.Title = "John Doe";
    newUser.Name = "Dr.";
    this.userList.Add(index,newUser);
    this.myUser=newUser;
    InitializeComponent();
  }
}

public class User
{
  public String Name { get; set; }
  public String Title { get; set; }
}

感谢任何建议。

首先,WPF 在您使用 MVVM, the general idea is implementing the INotifyPropertyChanged 时效果最佳,无论您添加到您的项目发生什么变化,都会传播到框架并更新您的视图。

使用列表时,对特定类型使用 ObservableCollection. If you want to add items dynamically to it, you would need to modify the ObservableCollection instead. For best results, in your UserControl, use a DataTemplate 以显示您的值的格式化版本。

对于第二部分,显示最后添加的项目,有几种方法可以解决这个问题,最好是添加一个可以保存数据的新项目(Grid、Stackpanel 等),使用 Binding将其值设置为与您的列表相同的上下文(即 ObservableCollection)并创建一个 Converter 将使用 ObservableCollection 作为输入,在您的特定转换器实现中,只需添加最后一项并将其显示到你想要的控制(你也可以为此使用数据模板)

解决方案:您必须在模型 class(User) 中绑定输入数据,并使用模型在列表框中插入数据,如下所示

<Window x:Class="WpfRegistration.Listbox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfRegistration"
    mc:Ignorable="d"
    Title="Listbox" Height="450" Width="800">
<Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.15*"></RowDefinition>
            <RowDefinition Height="0.85*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="63*"></ColumnDefinition>
            <ColumnDefinition Width="26*"></ColumnDefinition>
            <ColumnDefinition Width="109*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel>
            <TextBox x:Name="myName"  Text=""  Grid.Column="1"/>
            <TextBox x:Name="myTitle" Text=""  Grid.Column="0"/>
        </StackPanel>
        <StackPanel>
            <Button Height="23" Margin="50,40,0,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="76" Click="Button1_OnClick" >Add Item</Button>
        </StackPanel>
        <StackPanel>
            <Button Height="23" Margin="140,40,10,12" Name="DeleteButton" VerticalAlignment="Top" Click="DeleteButton_Click">Delete Item</Button>
        </StackPanel>
        <ListBox  Grid.Row="1" Grid.Column="0" BorderThickness="3" BorderBrush="Black" Margin="0,60,0,100"   x:Name="myUserList">
        </ListBox>
    </Grid>
</Grid>

Xaml.cs

public partial class Listbox : Window
{
    public Listbox()
    {
        InitializeComponent();
        User newUser = new User();
        newUser.Title = "John Doe";
        newUser.Name = "Dr.";
        this.myUserList.Items.Add(newUser.Title + newUser.Name);

    }

    private void Button1_OnClick(object sender, RoutedEventArgs e)
    {
      User newUser = new User();
        newUser.Title = myTitle.Text;
        newUser.Name = myName.Text;
        myUserList.Items.Add(newUser.Name + " " + newUser.Title );
    myTitle.Text=String.Empty;
        myName.Text=String.Empty;        
}

    private void DeleteButton_Click(object sender, RoutedEventArgs e)
    {
        myUserList.Items.RemoveAt(myUserList.Items.IndexOf(myUserList.SelectedItem));
    }
}
public class User
{
    public string name;
    public string title;

    public String Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged("name");
        }
    }

    public string Title
    {
        get { return title; }
        set
        {
            title = value;
            OnPropertyChanged("title");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}