我是WPF的新手。我有两个表SalesRep和Item,我有datagrid我需要在wpf datagrid中一次合并地显示两个表列。两个表都没有关系。
<Grid><DataGrid Name="salesrepGrid1" ItemsSource="{Binding SalesReps}" ItemSource="{Binding Items}" AutoGenerateColumns="False">
答案 0 :(得分:0)
正确答案可能不是您想要的答案,您需要构建一个能够正确建模数据的数据模型,如果您这样做,那么您不再需要多个来源,就像拥有单一数据模型一样
例如
XAML:
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<x:Array x:Key="team" Type="local:SalesTeam">
<local:SalesTeam>
<local:SalesTeam.Reps>
<local:SalesRep Name="A" Pay="5.2" Quota="50" />
<local:SalesRep Name="B" Pay="5.2" Quota="50" />
<local:SalesRep Name="C" Pay="5.2" Quota="50" />
</local:SalesTeam.Reps>
<local:SalesTeam.Items>
<local:SalesItem Name="Rocks" Qty="50" Value="1"/>
<local:SalesItem Name="Paper" Qty="50" Value="1"/>
<local:SalesItem Name="Sissros" Qty="50" Value="1"/>
</local:SalesTeam.Items>
</local:SalesTeam>
<local:SalesTeam >
<local:SalesTeam.Reps>
<local:SalesRep Name="1" Pay="5.2" Quota="50" />
<local:SalesRep Name="2" Pay="5.2" Quota="50" />
<local:SalesRep Name="3" Pay="5.2" Quota="50" />
</local:SalesTeam.Reps>
<local:SalesTeam.Items>
<local:SalesItem Name="Row" Qty="50" Value="1"/>
<local:SalesItem Name="Sham" Qty="50" Value="1"/>
<local:SalesItem Name="Bow" Qty="50" Value="1"/>
</local:SalesTeam.Items>
</local:SalesTeam>
</x:Array>
</Window.Resources>
<ListView ItemsSource="{Binding Mode=OneWay, Source={StaticResource team}}" >
<ListView.ItemTemplate>
<DataTemplate>
<TabControl>
<TabItem Header="Reps">
<DataGrid ItemsSource="{Binding Reps}" />
</TabItem>
<TabItem Header="Items">
<DataGrid ItemsSource="{Binding Items}"/>
</TabItem>
</TabControl>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Window>
型号:
public class SalesTeam
{
//details on team
public ObservableCollection<SalesRep> Reps { get; } = new ObservableCollection<SalesRep>();
public ObservableCollection<SalesItem> Items { get; } = new ObservableCollection<SalesItem>();
}
public class SalesRep : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
}
}
private int quota;
public int Quota
{
get { return quota; }
set
{
quota = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Quota)));
}
}
private double pay;
public double Pay
{
get { return pay; }
set
{
pay = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Pay)));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class SalesItem : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
}
}
private int qty;
public int Qty
{
get { return qty; }
set
{
qty = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Qty)));
}
}
private double val;
public double Value
{
get { return val; }
set
{
val = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
在这个例子中,我在XAML中构建了View Model,但你可以从任何地方获取它,这里我只是使用一组团队来填充Items控件并为每个数据网格模板化Tab控件