如何将列表中的列表绑定到Datagrid WPF

时间:2013-12-20 02:21:52

标签: wpf listview datagrid

我上课了BangLuong.cs

public partial class BangLuong
{
    public BangLuong()
    {
        this.LuongThuongs = new HashSet<LuongThuong>();
    }

    public int MaBangLuong { get; set; }
    public int MaNV { get; set; }
    public Nullable<decimal> TienLuong { get; set; }
    public Nullable<System.DateTime> Thang { get; set; }
    public string DonVi { get; set; }

    public virtual NhanVien NhanVien { get; set; }
    public virtual ICollection<LuongThuong> LuongThuongs { get; set; }
}

我希望将List<BangLuong>绑定到DataGridBangLuong列表LuongThuongs 当用户点击datagrid中的行时,它会显示列表LuongThuongs与此BangLuong匹配 我尝试在xaml中绑定:

 <DataGrid Grid.Row="2" ItemsSource="{Binding ListEntities, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridStyle}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Binding="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False">
                <DataGridCheckBoxColumn.HeaderTemplate>
                    <DataTemplate>
                        <CheckBox Command="{Binding SalaryDialog.CheckAllCommand}" DataContext="{StaticResource Locator}" CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}"/>
                    </DataTemplate>
                </DataGridCheckBoxColumn.HeaderTemplate>
            </DataGridCheckBoxColumn>
            <DataGridTextColumn Header="Mã bảng lương" Binding="{Binding MaBangLuong}" Width="Auto" IsReadOnly="True"/>
            <DataGridTextColumn Header="Tiền lương" Binding="{Binding TienLuong}" Width="Auto" IsReadOnly="True"/>
            <DataGridTextColumn Header="Tháng" Binding="{Binding Thang, Converter={StaticResource ConvertDataTimeToDateString}}" Width="Auto" IsReadOnly="True"/>
            <DataGridTextColumn Header="Trợ cấp" Binding="{Binding TroCap}" Width="*" IsReadOnly="True"/>
            <DataGridTextColumn Header="Đơn vị" Binding="{Binding DonVi}" Width="Auto" IsReadOnly="True"/>
            <DataGridTemplateColumn Header="Chức năng" Width="Auto">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Xóa" Command="{Binding SalaryDialog.DeleteCommand}" DataContext="{StaticResource Locator}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <ListView ItemsSource="{Binding LuongThuongs}">
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="Loại lương thưởng" DisplayMemberBinding="{Binding LoaiLuongThuong.TenLoaiLuong}" />
                            <GridViewColumn Header="Số tiền" DisplayMemberBinding="{Binding SoTien}" />
                        </GridView.Columns>
                    </GridView>
                </ListView>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>

它抛出了Xaml的异常。怎么做? 例外:发生了XamlParseException 消息:正在使用ItemsSource时操作无效。使用ItemsControl.ItemsSource访问和修改元素。

0 个答案:

没有答案