DataGrid.RowDetailsTemplate里面的字符串在添加到列表

时间:2015-12-11 09:16:35

标签: c# wpf xaml datagrid

这就是我的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类。在这个类中,我在datagrid行显示PersonalNumber和Name,但随后在RowDetailsTemplate中通过调用txtAccounts显示客户的Accounts。 添加客户时,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");
        }
    }


}

1 个答案:

答案 0 :(得分:0)

您应使用ObservableCollection<Customer>代替使用INotifyCollectionChanged, INotifyPropertyChanged进行DataGrid绑定,而ObservableCollection<T>会在添加,删除项目或刷新整个列表时提供通知(def edit @order = Order.find(params[:order_id]) @giftcard = @order.giftcard.where(giftcard_id params[:id]) end )。

有关<%= link_to edit_order_giftcard_path(@order), data: { modal: true } do %> <p>Edit card</p> <% end %> 的更多信息: https://msdn.microsoft.com/en-us/library/ms668604%28v=vs.110%29.aspx