WPF DataGrid不使用绑定的BindingList进行更新

时间:2018-01-09 09:06:07

标签: c# wpf xaml data-binding

我有一个绑定到DataGrid的BindingList,但是当我将项添加到该列表时,UI没有更新。

以下是可以重现此问题的最小代码版本:

MainWindow.xaml

<Window x:Class="WpfTestApp1.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:WpfTestApp1"
        mc:Ignorable="d"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <CollectionViewSource x:Key="SessionSource" Source="{Binding Sessions}" />
    </Window.Resources>
    <Grid>
        <DockPanel LastChildFill="True">
            <StackPanel DockPanel.Dock="Right">
                <Button x:Name="BtnTest" Click="BtnTest_OnClick" Content="Test"></Button>
            </StackPanel>
            <DataGrid x:Name="DG" DockPanel.Dock="Right" ItemsSource="{Binding Source={StaticResource SessionSource}}">
                <DataGrid.Columns>
                    <DataGridTextColumn x:Name="UserName" Width="Auto" Header="Title" Binding="{Binding Title}"/>
                    <DataGridTextColumn x:Name="UserAction" Width="Auto" Header="Host" Binding="{Binding Host}"/>
                </DataGrid.Columns>
            </DataGrid>
        </DockPanel>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfTestApp1
{
    public class Session : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _title;
        public string Title { get => _title; set { _title = value; OnPropertyChanged(); } }
        private string _host;
        public string Host { get => _host; set { _host = value; OnPropertyChanged(); } }
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public partial class MainWindow : Window
    {
        public BindingList<Session> Sessions { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            Sessions = new BindingList<Session>();
        }

        private void BtnTest_OnClick(object sender, RoutedEventArgs e)
        {
            Sessions.Add(new Session(){Title = "test1", Host="test2"});
        }
    }
}

尝试在MainWindow类中实现INotifyPropertyChanged接口,但似乎也不起作用。

1 个答案:

答案 0 :(得分:2)

在调用InitializeComponent之前初始化Sessions属性:

com.example.trng18.myapplication.Dept_login.savetodb(Dept_login.java:144)

在WPF中,没有必要使用BindingList,尤其是在元素类型实现INotifyPropertyChanged时。 ObservableCollection更常见:

public partial class MainWindow : Window
{
    public BindingList<Session> Sessions { get; } = new BindingList<Session>();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void BtnTest_OnClick(object sender, RoutedEventArgs e)
    {
        Sessions.Add(new Session { Title = "test1", Host = "test2" });
    }
}