DataGrid を、IMyInterface から派生したさまざまな型のオブジェクトのコレクションにバインドします。オブジェクトの実際のタイプによって DataGrid をグループ化し、各タイプのプロパティに基づいて各グループ内で列を自動生成する必要があります。タイプには異なるプロパティがあるためです。
助けてください。
既存のコードの一部を追加しています:
XAML:
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False"
Background="White"
Foreground="Black">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Path=ItemCount}" Padding="10, 0, 10, 0"/>
<TextBlock Text="Item(s)"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
...
<DataGrid Grid.Row="1" Grid.Column="2" Name="DgPendingMessages" ItemsSource="{Binding }" AutoGenerateColumns="False"
IsReadOnly="True" MaxHeight="400">
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter />
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding TheMessage}" Header="Pending Message" />
</DataGrid.Columns>
</DataGrid>
コード ビハインド:
public class PendingMessage
{
//public IMyInterface TheMessage { get; set; }
public string TheMessage { get; set; }
public string MessageType { get; set; }
}
PendingMessage は、実際の Message オブジェクトの文字列表現と Message オブジェクトの元の型 (それによって DataGrid をグループ化するために使用される) を含むモデル クラスです。
Message オブジェクトは、IMyInterface から派生した任意の型にすることができます。
var collectionPendingMessages = new ListCollectionView(PendingMessages);
collectionPendingMessages.GroupDescriptions.Add(new PropertyGroupDescription("MessageType"));
DgPendingMessages.ItemsSource = collectionPendingMessages;
実際の Message オブジェクトの文字列表現の代わりに、次のようなものが必要です。
タイプA:3品
列 N | 列 Z | 列 X | T列
データ | データ データ | データ データ | データ データ
データ | データ データ | データ データ | データ データ
データ | データ データ | データ データ | データ データ
タイプB:4品
列 N | S列 | 列 U
データ | データ データ | データ データ
データ | データ データ | データ データ
データ | データ データ | データ データ
データ | データ データ | データ データ
タイプC:3点
列 N | 列 Z | 列 X | T列 | 列 U
データ | データ データ | データ データ | データ データ | データ データ
データ | データ データ | データ データ | データ データ | データ データ
データ | データ データ | データ データ | データ データ | データ データ