DataGrid.RowDetailsTemplate中的字符串添加到列表时未更新

本文关键字:列表 更新 添加 RowDetailsTemplate 字符串 DataGrid | 更新日期: 2023-09-27 18:26:33

这就是我的XAML的外观:

<Grid>
    <DataGrid x:Name="dataGCustomer" HorizontalAlignment="Left" Margin="25,51,0,0" VerticalAlignment="Top" Height="217" Width="490" Loaded="dataGCustomer_Loaded" AutoGenerateColumns="False" SelectionMode="Single" SelectionChanged="dataGCustomer_SelectionChanged" CanUserResizeRows="False" CanUserAddRows="False" ItemsSource="{Binding Path=customerList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding PersonalNumber, UpdateSourceTrigger=PropertyChanged}" ClipboardContentBinding="{x:Null}" Header="Personnummer" IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" ClipboardContentBinding="{x:Null}" Header="Namn" IsReadOnly="True"/>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding txtAccounts, UpdateSourceTrigger=PropertyChanged}" Margin="10"/>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>
    <Button x:Name="btnNewCust" Content="Ny kund" HorizontalAlignment="Left" Margin="25,280,0,0" VerticalAlignment="Top" Width="75" Click="btnNewCust_Click"/>
    <Button x:Name="btnChangeCust" Content="Ändra kund" HorizontalAlignment="Left" Margin="119,280,0,0" VerticalAlignment="Top" Width="75" Click="btnChangeCust_Click" IsEnabled="False"/>
    <Button x:Name="btnRemoveCust" Content="Ta bort kund" HorizontalAlignment="Left" Margin="213,280,0,0" VerticalAlignment="Top" Width="75" Click="btnRemoveCust_Click" IsEnabled="False"/>
    <Button x:Name="btnHandleAccount" Content="Hantera konton" HorizontalAlignment="Left" Margin="395,280,0,0" VerticalAlignment="Top" Width="99" Click="btnHandleAccount_Click" IsEnabled="False"/>
    <TextBox x:Name="txtBoxSearch" HorizontalAlignment="Left" Height="23" Margin="25,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="textBox_TextChanged"/>
    <Label x:Name="lblSearchCustomer" Content="Sök kund (namn eller personnummer)" HorizontalAlignment="Left" Margin="24,3,0,0" VerticalAlignment="Top" FontSize="9.333" FontStyle="Italic" Background="#FFF7F5F5" Foreground="#FFACA0A0"/>
    <Label x:Name="lblLogo" Content="Newton Bank" HorizontalAlignment="Left" Margin="337,10,0,0" VerticalAlignment="Top" Width="178" Height="36" FontFamily="Vani" FontSize="24" Foreground="#FFB61414" FontWeight="Bold"/>
</Grid>

这是XAML的main.cs的一部分代码:

    public MainWindow()
    {
        InitializeComponent();
        customerList = new List<Customer>();
        customerList = bankLogic.GetAllCustomers();
        dataGCustomer.ItemsSource = customerList;
    }

这是Customer类。在这个类中,我在数据网格行中显示PersonalNumber和Name,但在RowDetailsTemplate中,我通过调用txtCounts来显示客户的帐户。添加客户时,RowDetailTemplate不会在数据网格上刷新。

public class Customer: INotifyPropertyChanged {
    string text = "";
    public event PropertyChangedEventHandler PropertyChanged; 
protected void OnPropertyChanged(string xName)
    {
        PropertyChangedEventHandler h = PropertyChanged;
        if (h == null) return;
        h(this, new PropertyChangedEventArgs(xName));
    }
    public Customer(long pNr, string name)
    {
        PersonalNumber = pNr;
        Name = name;
        Accounts = new List<Account>();
    }
    public Customer()
    {
    }
    // Egenskaper
    //public int CustomerID { get; set; } // PK
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long PersonalNumber { get; set; }
    [Required]
    [MaxLength(50, ErrorMessage = "Totalt 50 tecken")]
    public string Name { get; set; }
    public virtual List<Account> Accounts { get; set; } // Sammankoppla
    [NotMapped]
    public string txtAccounts
    {
        get
        {
            if (Accounts.Count > 0)
            {
                foreach (var item in Accounts)
                {
                    text += item.ToString() + "'n";
                }
                return text;
            }
            else
                return "Konton saknas";
        }
        set
        {
            text = value;
            OnPropertyChanged("txtAccounts");
        }
    }

}

DataGrid.RowDetailsTemplate中的字符串添加到列表时未更新

不应将List<Customer>用于DataGrid绑定,而应使用ObservableCollection<Customer>,它在添加、删除项目或刷新整个列表(INotifyCollectionChanged, INotifyPropertyChanged)时提供通知。

关于ObservableCollection<T>的更多信息:https://msdn.microsoft.com/en-us/library/ms668604%28v=vs.110%29.aspx