.Xaml
DataContext="{DynamicResource ViewModelCombine}">
<Window.Resources>
<vm:ViewModelCombine x:Key="ViewModelCombine"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid x:Name="grd">
<DataGrid.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource ViewModelCombine}, Path=MergedSource}"/>
</CompositeCollection>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridTextColumn Header="AMP" Binding="{Binding AMP}" Width="100"/>
<DataGridTextColumn Header="PW" Binding="{Binding PW}" Width="100" />
<DataGridTextColumn Header="DZ0" Binding="{Binding DZ0}" Width="100" />
<DataGridTextColumn Header="DELTA" Binding="{Binding DELTA}" Width="100" />
<DataGridTextColumn Header="DZ1" Binding="{Binding DZ1}" Width="100"/>
<DataGridTextColumn Header="M" Binding="{Binding M_View}" Width="100" />
<DataGridTextColumn Header="DZ2" Binding="{Binding DZ2}" Width="100" />
<DataGridTextColumn Header="N" Binding="{Binding N}" Width="100" />
</DataGrid.Columns>
</DataGrid>
</Grid>
.Xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModelCombine VMC = new ViewModelCombine();
grd.DataContext = VMC;
}
}
ViewModelCombine.cs
public class ViewModelCombine
{
private ObservableCollection<TherapyTiming> secondsource;
public ObservableCollection<TherapyTiming> SecondSource
{
get { return secondsource; }
set { secondsource = value; }
}
private ObservableCollection<PulseTiming> firstsource;
public ObservableCollection<PulseTiming> FirstSource
{
get { return firstsource; }
set { firstsource = value; }
}
private CompositeCollection mergedSource;
public CompositeCollection MergedSource
{
get { return mergedSource; }
set { mergedSource = value; OnPropertyChanged("MergedSource"); }
}
public ViewModelCombine()
{
var secondinfo = new ViewModelTherapy();
SecondSource = secondinfo;
var firstinfo = new ViewModelPulse();
FirstSource = firstinfo;
mergedSource = new CompositeCollection();
CollectionContainer collection1 = new CollectionContainer() { Collection = SecondSource };
CollectionContainer collection2 = new CollectionContainer() { Collection = FirstSource };
mergedSource.Add(collection1);
mergedSource.Add(collection2);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
使用 CompositeCollection 将 Datagrid 绑定到 MergedCollection 作为 itemsource 时出现问题。在两行而不是一行中显示数据并在此处检查输出。
要求是在一行中显示所有列...!谢谢。