确定要将属性添加到树视图中的类的类型

时间:2014-05-06 00:56:57

标签: c# wpf treeview datatemplate

我有一个类似这样的课程:

public class PLOAMD_STRUCT
    {
        public ushort ONU_ID { get; set; }                   
        public byte MessageType { get; private set; }               
        public byte SequenceNumber { get; set; }            
        public PLOAM_Messages.MESSAGE MessageContent { get; private set; }      
        public ulong MIC { get; set; }
    }

MessageContent属于抽象类:

public abstract class MESSAGE
    {

    }

内容可以是许多类中的一个,它扩展如下:

public class PROFILE_MESSAGE : MESSAGE
    {
        public byte Version { get; set; }
        public byte Profile { get; set; }
        public bool FEC { get; set; }
        public byte Delimiter { get; set; }
        public byte Preamble_Length { get; set; }
        public ulong Preamble { get; set; }
        public ulong PON_Tag { get; set; }
    }

public class SERIAL_NUMBER_GRANT_MESSAGE : MESSAGE
    {
        public uint Vendor_ID { get; set; }
        public uint Vendor_SSN { get; set; }
        public uint Random_Delay { get; set; }
    }

在XAML中,我需要确定一种类,并根据类型显示其属性。我有这个:

<TreeViewItem Header="PLOAMd[]" ItemsSource="{Binding Path=Payload.Header.PLOAMd}">
                                        <TreeViewItem.ItemTemplate>
                                            <DataTemplate>
                                                <Border BorderBrush="Black" BorderThickness="1">
                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*" />
                                                        <ColumnDefinition Width="100" />
                                                        <ColumnDefinition Width="*" />
                                                    </Grid.ColumnDefinitions>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition />
                                                        <RowDefinition />
                                                        <RowDefinition />
                                                        <RowDefinition />
                                                        <RowDefinition />
                                                        </Grid.RowDefinitions>
                                                    <TextBlock Text="ONU ID" Grid.Column="0" Grid.Row="0" />
                                                    <TextBlock Text="{Binding Path=ONU_ID}" Grid.Column="2" Grid.Row="0" />
                                                    <TextBlock Text="Message type" Grid.Column="0" Grid.Row="1" />
                                                    <TextBlock Text="{Binding Path=MessageType}" Grid.Column="2" Grid.Row="1" />
                                                    <TextBlock Text="Sequence number" Grid.Column="0" Grid.Row="2" />
                                                    <TextBlock Text="{Binding Path=SequenceNumber}" Grid.Column="2" Grid.Row="2" />
                                                        <TreeViewItem Header="Message content" Grid.Column="0" Grid.Row="3">
                                                            <TreeViewItem.Items>
                                                                <!-- Content HERE! -->
                                                                <TextBlock Text="{Binding Path=MessageContent.Version}" />
                                                            </TreeViewItem.Items>
                                                        </TreeViewItem>
                                                    <TextBlock Text="MIC" Grid.Column="0" Grid.Row="4" />
                                                    <TextBlock Text="{Binding Path=MIC}" Grid.Column="2" Grid.Row="4" />
                                                </Grid>
                                                </Border>
                                            </DataTemplate>
                                        </TreeViewItem.ItemTemplate>

                                    </TreeViewItem>

如果我使用评论下的代码:

<TextBlock Text="{Binding Path=MessageContent.Version}" />

并且消息的类型是PROFILE_MESSAGE它似乎自动重新键入它并给我正确的价值。但是如何做到它将选择正确的结构并显示它。因此,如果邮件的类型为SERIAL_NUMBER_GRANT_MESSAGE,我只会看到:

Vendor_ID
Vendor_SSN
Random_Delay

等等。块的构造很容易我只需要选择一个可能的模板并显示它。

有什么建议吗?

0 个答案:

没有答案