0

在我的应用程序中,我显示了来自社交网络 vkontakte(俄罗斯 facebook:) 的新闻源。我向服务器发送请求并接收一个 xml 文件,其中包含大约 50 个新闻源项目。xml 示例(2 项,共 50 项)

    <item>
  <type>post</type>
  <source_id>-26406986</source_id>
  <date>1356207001</date>
  <post_id>1058858</post_id>
  <text>#Суть на #lm.</text>
  <attachment>
    <type>photo</type>
    <photo>
      <pid>293678498</pid>
      <aid>-7</aid>
      <owner_id>-26406986</owner_id>
      <user_id>65647811</user_id>
      <src>http://cs406928.userapi.com/v406928811/61c7/Rjee0rUq3vU.jpg</src>
      <src_big>http://cs406928.userapi.com/v406928811/61c8/PSYiLcxlv9s.jpg</src_big>
      <src_small>http://cs406928.userapi.com/v406928811/61c6/26XXAbejR7U.jpg</src_small>
      <width>186</width>
      <height>604</height>
      <text />
      <created>1356203380</created>
      <access_key>ca379f61ca5de866ae</access_key>
    </photo>
  </attachment>
  <attachments list="true">
    <attachment>
      <type>photo</type>
      <photo>
        <pid>293678498</pid>
        <aid>-7</aid>
        <owner_id>-26406986</owner_id>
        <user_id>65647811</user_id>
        <src>http://cs406928.userapi.com/v406928811/61c7/Rjee0rUq3vU.jpg</src>
        <src_big>http://cs406928.userapi.com/v406928811/61c8/PSYiLcxlv9s.jpg</src_big>
        <src_small>http://cs406928.userapi.com/v406928811/61c6/26XXAbejR7U.jpg</src_small>
        <width>186</width>
        <height>604</height>
        <text />
        <created>1356203380</created>
        <access_key>ca379f61ca5de866ae</access_key>
      </photo>
    </attachment>
  </attachments>
  <comments>
    <count>47</count>
    <can_post>1</can_post>
  </comments>
  <likes>
    <count>167</count>
    <user_likes>0</user_likes>
    <can_like>1</can_like>
    <can_publish>1</can_publish>
  </likes>
  <reposts>
    <count>35</count>
    <user_reposted>0</user_reposted>
  </reposts>
  <post_source>
    <type>vk</type>
  </post_source>
</item>

每个项目都可以有不同的内容<attachments>(文本、照片、视频、音频、文档、文本 n 照片、文本 n 音频以及该五线谱的所有可能组合)。我已经在这里问过这个问题。我有一个答案 - 使用模板选择器。嗯……我用过,现在我有 16 种不同listbox的物品,而且不是所有可能的组合。它在 xaml 中的代码非常庞大。示例,2 个模板:

视频和音频

      <!--VideonAudios-->          <local:NewsTemplateSelector.VideonAudios>
                                <DataTemplate>
                                    <Grid Name="VideoGrid" MaxHeight="2000" Tap="ListBoxTap"   >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="75"/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="75"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="28"/>
                                        </Grid.RowDefinitions>
                                        <Image Source="{Binding SourceImage}" Tap="GetUserInfo"  Width="75"/>
                                        <TextBlock Text="{Binding SourceName}"   FontSize="25" Grid.Column="1" TextWrapping="Wrap" />
                                        <TextBlock Grid.Column="1" TextWrapping="Wrap" Text="{Binding Copy_owner_name}"    FontSize="25" VerticalAlignment="Bottom" Margin="39,0,0,0" MaxHeight="35"/>
                                        <Image Source="{Binding Copy_owner_photo}" Grid.Column="1" HorizontalAlignment="Left" Height="35" Width="39" VerticalAlignment="Bottom" />
                                        <TextBlock Text="{Binding Video[0].Title}"    FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap"  VerticalAlignment="Top" Height="auto" />
                                        <Image Source="{Binding Video[0].Big}" Margin="10" Grid.Column="1" Grid.Row="2" Tap="Video_Tap"  />
                                        <ListBox Name="audiosListbox" ItemsSource="{Binding Audio}"  MaxHeight="500" Margin="10" Grid.Column="1" Grid.Row="3" >
                                            <ListBox.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid>
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="100"/>
                                                            <ColumnDefinition/>
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Grid.Column="1"   FontSize="25" Tap="AudioTitleTap" />
                                                        <Button Content="Play"  Height="70" Tap="Audios_Button_Click" />
                                                        <TextBox Text="{Binding Count}" Tag="Count" Visibility="Collapsed" />
                                                    </Grid>
                                                </DataTemplate>
                                            </ListBox.ItemTemplate>
                                        </ListBox>
                                        <Image Source="\icons\appbar.check.rest.png" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="4" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
                                        <TextBlock Text="{Binding Time}" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="4"  />
                                        <TextBlock Text="{Binding Likes}" Name="LikeTxtBox" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="4"   TextWrapping="Wrap" Width="30" Margin="0,0.5,0,-0.5"/>
                                        <Line X1="0" Y1="27" X2="420" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" Grid.Column="1"  />
                                        <Line X1="0" Y1="27" X2="80" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" />
                                        <TextBox Text="{Binding Video[0].Url}" Tag="VURL" Visibility="Collapsed" />
                                    </Grid>
                                </DataTemplate>
                            </local:NewsTemplateSelector.VideonAudios>

它只有 16 个模板中的 2 个。嗯......它有效。 1

2

3

但是,有时列表框会以一些奇怪的行为开始工作,我认为这是因为这个模板。现在,我必须在不同的页面上制作类似的列表框,我认为使用模板选择器不是一个好主意。谁能告诉我,有没有不同的方法来构建listbox具有不同内容的项目?

UPD:我解析 xml 的方式

 void c_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
      //  string str = null;
        XDocument xml = XDocument.Load(e.Result); //полученый xml от апи вконтакта
        MessageBox.Show(xml.ToString());
        var inf = from c in xml.Descendants("item") // 
                  select c;
        int j = -1;
        int i = 0;
        App.New_From = (string)xml.Element("response").Element("new_from").Value;
        foreach (var item in inf)
        {
            #region static info
            i++;
            if (inf.Count() > i)
            {
                if ((string)item.Element("type").Value != "friend")
                {
                    New new1 = new New();
                    if ((string)item.Element("source_id").Value != null)
                    {
                        new1.SourseId = (string)item.Element("source_id").Value;
                    }
                    if ((string)item.Element("date").Value != null)
                    {
                       // MessageBox.Show(item.Element("date").Value);
                        new1.Time = App.GetTime(item.Element("date").Value);
                      //  MessageBox.Show(App.GetTime(item.Element("date").Value));
                    }
                    if (item.Element("photos") != null)
                    {
                        var photos = from c in item.Descendants("photo") // 
                                     select c;
                        foreach (var ph in photos)
                        {
                            PhotoAttachment photo = new PhotoAttachment();
                            photo.OwnerId = ph.Element("owner_id").Value;
                            photo.PId = ph.Element("pid").Value;
                            photo.Small = ph.Element("src_small").Value;
                            photo.Big = ph.Element("src_big").Value;
                            if (ph.Element("src_xxbig") != null)
                                photo.xBig = ph.Element("src_xxbig").Value;
                            else if (ph.Element("src_xbig") != null)
                                photo.xBig = ph.Element("src_xbig").Value;
                            else
                                photo.xBig = ph.Element("src_big").Value;
                            new1.photoAttachments.Add(photo);
                        }
                    }
                    if (item.Element("copy_owner_id") != null)
                    {
                        new1.Copy_owner_id = (string)item.Element("copy_owner_id").Value;
                    }
                    if (item.Element("post_id") != null)
                    {
                        new1.Post_id = (string)item.Element("post_id").Value;
                    }
                    if ((string)item.Element("text") != null)
                    {
                        new1.Texts = (string)item.Element("text");
                    }
                    if ((string)item.Element("comment") != null)
                        new1.Comments = (string)item.Element("comment").Element("count").Value;
                    if ((string)item.Element("likes") != null)
                        new1.Likes = (string)item.Element("likes").Element("count").Value;
            #endregion
                    if (item.Element("attachment") != null)
                    {
                        var vd = from c in item.Descendants("attachment")
                                 select c;
                        foreach (var content in vd)
                        {

                            if (content.Element("audio") != null)
                            {
                                AudioAttachment audio = new AudioAttachment();
                                audio.Title = (string)content.Element("audio").Element("title").Value;
                                audio.Perfomer = (string)content.Element("audio").Element("performer").Value;
                                string tmp = audio.Perfomer + " - " + audio.Title;
                                audio.Title = tmp;
                                audio.OwnerId = (string)content.Element("audio").Element("owner_id").Value;
                                audio.Audio_Id = (string)content.Element("audio").Element("aid").Value;
                                audio.Duration = (string)content.Element("audio").Element("duration").Value;
                                audio.Url = audio.OwnerId + "_" + audio.Audio_Id;
                                audio.Count = j++;
                                if (new1.audioAttachments.Count > 0)
                                {
                                    if (audio.Audio_Id == new1.audioAttachments[0].Audio_Id)
                                    {
                                        new1.audioAttachments.RemoveAt(0);
                                    }
                                }
                                new1.audioAttachments.Add(audio);
                            }
                            if (content.Element("photo") != null)
                            {
                                PhotoAttachment photo = new PhotoAttachment();
                                photo.OwnerId = (string)content.Element("photo").Element("owner_id").Value;
                                photo.PId = (string)content.Element("photo").Element("pid").Value;
                                photo.Small = (string)content.Element("photo").Element("src_small").Value;
                                photo.Big = (string)content.Element("photo").Element("src_big").Value;
                                if (content.Element("photo").Element("src_xxbig") != null)
                                    photo.xBig = (string)content.Element("photo").Element("src_xxbig").Value;
                                else if (content.Element("photo").Element("src_xbig") != null)
                                    photo.xBig = (string)content.Element("photo").Element("src_xbig").Value;
                                else
                                    photo.xBig = (string)content.Element("photo").Element("src_big").Value;
                                if (new1.photoAttachments.Count > 0)
                                {
                                    if (photo.Big == new1.photoAttachments[0].Big)
                                    {
                                        new1.photoAttachments.RemoveAt(0);
                                    }
                                }
                                new1.photoAttachments.Add(photo);
                            }

                            if (content.Element("video") != null)
                            {
                                VideoAttachment video = new VideoAttachment();
                                video.Title = (string)content.Element("video").Element("title").Value;
                                video.Description = (string)content.Element("video").Element("description").Value;
                                video.OwnerId = (string)content.Element("video").Element("owner_id").Value;
                                if ((string)content.Element("video").Element("image_small") != null)
                                    video.Small = (string)content.Element("video").Element("image_small").Value;
                                if ((string)content.Element("video").Element("image_big") != null)
                                    video.Big = (string)content.Element("video").Element("image_big").Value;
                                video.Video_Id = (string)content.Element("video").Element("vid").Value;
                                video.Url = video.OwnerId + "_" + video.Video_Id;
                                if (new1.videoAttachments.Count > 0)
                                {
                                    if (video.Big == new1.videoAttachments[0].Big)
                                    {
                                        new1.videoAttachments.RemoveAt(0);
                                    }

                                }
                                new1.videoAttachments.Add(video);
                            }

                            if (content.Element("link") != null)
                            {
                                new1.Priority = PostType.Link;
                                new1.Url.Title = (string)content.Element("link").Element("title").Value;
                                new1.Url.Description = (string)content.Element("link").Element("description").Value;
                                new1.Url.Url = (string)content.Element("link").Element("url").Value;
                                if ((string)content.Element("link").Element("image_src") != null)
                                    new1.Url.Image = (string)content.Element("link").Element("image_src").Value;
                                else if (new1.photoAttachments.Count > 0 && new1.photoAttachments[0].Big != null)
                                    new1.Url.Image = new1.photoAttachments[0].Big;
                                else if (new1.photoAttachments.Count > 0 && new1.photoAttachments[1].Big != null)
                                    new1.Url.Image = new1.photoAttachments[1].Big;

                            }
                        }
                    }

                    if (item.Element("comments") != null)
                    {
                        if (item.Element("comments").Element("can_post") != null)
                            new1.CanPost = Convert.ToInt32(item.Element("comments").Element("can_post").Value);
                    }


                    //str += string.Format("{0} {1} {2}\n", NewsList.Count, new1.audioAttachments.Count, new1.photoAttachments.Count);

                    if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Text;
                    if (new1.photoAttachments.Count == 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Photo;
                    if (new1.photoAttachments.Count > 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Photos;
                    if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 1 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Video;
                    if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count > 1 && new1.audioAttachments.Count == 0 && new1.urlAttachment.Url == null) new1.Priority = PostType.Videos;
                    if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.Audio;
                    if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.Audios;
                    if (new1.photoAttachments.Count == 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.PhotonAudio;
                    if (new1.photoAttachments.Count == 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.PhotonAudios;
                    if (new1.photoAttachments.Count > 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.PhotosnAudio;
                    if (new1.photoAttachments.Count > 1 && new1.videoAttachments.Count == 0 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.PhotosnAudios;
                    if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 1 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.VideonAudio;
                    if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count == 1 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.VideonAudios;
                    if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count > 1 && new1.audioAttachments.Count == 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.VideosnAudio;
                    if (new1.photoAttachments.Count == 0 && new1.videoAttachments.Count > 1 && new1.audioAttachments.Count > 1 && new1.urlAttachment.Url == null) new1.Priority = PostType.VideosnAudios;


                    NewsList.Add(new1);
                    new1 = null;
                }
            }

我可以做类似的事情吗

    <local:NewsTemplateSelector.Content.Audios>
<ListBox Name="audiosListbox" ItemsSource="{Binding Audio}"  MaxHeight="500" Margin="10" Grid.Column="1" Grid.Row="3" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Grid.Column="1"   FontSize="25" Tap="AudioTitleTap" />
                <Button Content="Play"  Height="70" Tap="Audios_Button_Click" />
                <TextBox Text="{Binding Count}" Tag="Count" Visibility="Collapsed" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
<local:NewsTemplateSelector.Content.Audios>

和图像、视频、视频等类似。然后像这样使用它

<!--Audios-->
<local:NewsTemplateSelector.Audios>
<DataTemplate>
    <Grid Name="AudiosGrid" MaxHeight="2000" Tap="ListBoxTap"    >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="75"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="75"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="28"/>
        </Grid.RowDefinitions>
        <Image Source="{Binding SourceImage}" Tap="GetUserInfo"  Width="75"/>
        <TextBlock Text="{Binding SourceName}"   FontSize="25" Grid.Column="1" TextWrapping="Wrap" />
        <TextBlock Grid.Column="1" TextWrapping="Wrap" Text="{Binding Copy_owner_name}"    FontSize="25" VerticalAlignment="Bottom" Margin="39,0,0,0" MaxHeight="35"/>
        <Image Source="{Binding Copy_owner_photo}" Grid.Column="1" HorizontalAlignment="Left" Height="35" Width="39" VerticalAlignment="Bottom" />
        <TextBlock Text="{Binding Texts}"    FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap"  VerticalAlignment="Top" Height="auto" />
        <local:NewsTemplateSelector.Content.Audios>
        </local:NewsTemplateSelector.Content.Audios>
        <Image Source="\icons\appbar.check.rest.png" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
        <TextBlock Text="{Binding Time}" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="3"  />
        <TextBlock Text="{Binding Likes}" Name="LikeTxtBox" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3"   TextWrapping="Wrap" Width="30" Margin="0,0.5,0,-0.5"/>
        <Line X1="0" Y1="27" X2="420" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" Grid.Column="1"  />
        <Line X1="0" Y1="27" X2="80" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" />
    </Grid>
</DataTemplate>

不是

<!--Audios-->
<local:NewsTemplateSelector.Audios>
<DataTemplate>
    <Grid Name="AudiosGrid" MaxHeight="2000" Tap="ListBoxTap"    >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="75"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="75"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="28"/>
        </Grid.RowDefinitions>
        <Image Source="{Binding SourceImage}" Tap="GetUserInfo"  Width="75"/>
        <TextBlock Text="{Binding SourceName}"   FontSize="25" Grid.Column="1" TextWrapping="Wrap" />
        <TextBlock Grid.Column="1" TextWrapping="Wrap" Text="{Binding Copy_owner_name}"    FontSize="25" VerticalAlignment="Bottom" Margin="39,0,0,0" MaxHeight="35"/>
        <Image Source="{Binding Copy_owner_photo}" Grid.Column="1" HorizontalAlignment="Left" Height="35" Width="39" VerticalAlignment="Bottom" />
        <TextBlock Text="{Binding Texts}"    FontSize="25" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap"  VerticalAlignment="Top" Height="auto" />
        <ListBox Name="audiosListbox" ItemsSource="{Binding Audio}"  MaxHeight="500" Margin="10" Grid.Column="1" Grid.Row="2" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Grid.Column="1"   FontSize="25" Tap="AudioTitleTap" />
                        <Button Content="Play"  Height="70" Tap="Audios_Button_Click" />
                        <TextBox Text="{Binding Count}" Tag="Count" Visibility="Collapsed" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Image Source="\icons\appbar.check.rest.png" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3" Width="30" Margin="0,1.5,34,-1.5" d:LayoutOverrides="VerticalMargin"/>
        <TextBlock Text="{Binding Time}" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="3"  />
        <TextBlock Text="{Binding Likes}" Name="LikeTxtBox" Grid.Column="1" HorizontalAlignment="Right" Grid.Row="3"   TextWrapping="Wrap" Width="30" Margin="0,0.5,0,-0.5"/>
        <Line X1="0" Y1="27" X2="420" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" Grid.Column="1"  />
        <Line X1="0" Y1="27" X2="80" Y2="27" Margin="0,-28,0,0" Stroke="Gray" StrokeThickness="1" />
    </Grid>
</DataTemplate>

4

1 回答 1

0

通常,具有完全不同内容的每个 XML 组件(例如视频与图像,或者只是文本等)都需要不同的模板。但是您应该将模板定义到不同的 XAML 文件中,并将其作为资源字典添加到每个控件中。这样,您可以在 1 个地方定义模板并将它们导入所有控件,而无需复制/粘贴任何内容。您甚至可以使用MergedDictionary.

我不确定您是如何从该站点读取 XML 文件的,但也许您应该将它们解析为代码隐藏中的自定义对象,在更简单的类别中?这也会给您的应用程序带来更流畅的感觉。否则,在代码隐藏中解析 XML,识别它是什么类型的内容并填充您自己的“视频”、“图像”和“文本”对象。如果您可以将许多不同的 XML 部分分类到同一个对象中,那么从长远来看,您将不得不依赖更少的模板选择器/数据模板。

但是当然我在推测很多事情,希望这对您有所帮助。

编辑:

  1. 不要在每个控件上手动提供“视觉”样式,而是在资源字典中定义样式并将默认字体大小、对齐方式、文本换行首选项等内容放入其中。然后你可以像这样使用它:Style="{StaticResource=TextCommentStyle}".
  2. 对于您的视频/音频/等控件,为您需要显示的每种类型的项目创建一个容器。每个不同的项目都应该有一个不同的 UserControl,它充当容器。
  3. 然后,您可以使用模板选择器根据每个附件的标记仅显示您想要的 UserControl。我相信每个都应该托管在一个单独的 ListItem 中。

使用这种方法的优点是您将拥有更少的容器,并且您将让应用程序使用模板选择器“组合”它们。如果有视频和一些文本,那么数据模板应该使用它们各自的用户控件将视频放在 ListItem 中,将文本放在另一个 ListItem 中。如果您需要编辑视频的显示方式,您只需编辑 1 个视频 UserControl,它就会反映在整个应用程序中。WPF 控件的样式与此想法相同:将它们设置为样式,然后您可以在单个文件中全局更改它们。它更容易维护和调试。

于 2012-12-23T16:13:15.987 回答