0

我最近刚刚开始开发一个在 listBox 中显示一堆 xml 数据的应用程序,但问题不是每个 xml 项目都有一个子值(在我的问题中,上传),所以它的 listBox 代码不应该显示.

例子:

<item>
     <id>1</id>
     <body>Some text</body>
     <upload></upload>
     <created>Some text</created>
</item>

我正在获取数据并以这种方式填充我的列表:

var data = from query in loadedData.Descendants("item")
                   select new droppedItem
                   {
                       Id = (int)query.Element("id"),
                       Body = (string)query.Element("body"),
                       Upload = (string)query.Element("upload"),
                       Created = (DateTime)ConvertFromUnixTimestamp((double)query.Element("created"))
                   };
        userDrops.ItemsSource = data;

我的 xaml 看起来像:

<ListBox Margin="0,0,-12,0" Name="userDrops">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432">
                            <RichTextBox>
                                <Paragraph>
                                    <Run Text="{Binding Body}" FontSize="25" FontFamily="Segoe WP"></Run>
                                </Paragraph>
                                <Paragraph>
                                    <Hyperlink NavigateUri="{Binding Upload}" TargetName="_blank" FontSize="25" FontFamily="Segoe WP">{Binding Upload}</Hyperlink>
                                </Paragraph>
                                <Paragraph>
                                    <Run Text="{Binding Created}" FontFamily="Segoe WP SemiLight"></Run>
                                </Paragraph>
                            </RichTextBox>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

如果 xml 上传子项持有值,我想要做的只是显示持有上传信息的段落。否则,只需将其从列表框中删除。我不知道如何更改基本的 listBox 模板。

任何帮助都会很棒!

4

1 回答 1

0

使用TextBlocks显示 Body 和 Created 怎么样?中间部分是 a RichTextBox,如果 Upload 为空,您可以使用 Visibility 转换器切换隐藏/显示。

您需要先将转换器包含在您的 xaml 页面中,

xmlns:converters="clr-namespace:xxx.Converters;assembly=xxx"

<converters:VisibilityConverter x:Key="visibilityConverter" />

然后在你的模板中使用它,

                <StackPanel Margin="40,237,8,326" Width="432" Grid.Row="1"> 
                    <RichTextBox> 
                        <Paragraph> 
                            <Run Text="{Binding Body}" FontSize="25" FontFamily="Segoe WP"></Run> 
                        </Paragraph>
                    </RichTextBox> 
                    <RichTextBox Visibility="{Binding Upload, Converter={StaticResource VisibilityConverter}}">
                        <Paragraph> 
                            <Hyperlink NavigateUri="{Binding Upload}" TargetName="_blank" FontSize="25" FontFamily="Segoe WP">{Binding Upload}</Hyperlink> 
                        </Paragraph>
                    </RichTextBox> 
                        <TextBlock Text="{Binding Created}" FontFamily="Segoe WP SemiLight"/>
                </StackPanel>

转换器应该是这样的,

  public class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool visible = true;

            if (value is bool)
            {
                visible = (bool)value;
            }
            else if (value is int || value is short || value is long)
            {
                visible = 0 != (int)value;
            }
            else if (value is float || value is double)
            {
                visible = 0.0 != (double)value;
            }
            else if (value is string) {
                visible = ((string)value).Length > 0;
            }
            else if (value == null) {
                visible = false;
            }

            if ((string)parameter == "!")
            {
                visible = !visible;
            }

            return visible ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

有关值转换器的更多信息,请参阅

更新

在您的 App.xaml 中,执行此操作,

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:sg="http://schemas.stargategroup.com.au/2010/xaml/presentation" 
             xmlns:converters="clr-namespace:xxx.Converters"
             x:Class="xxx.Application.App">

    <Application.Resources>
        <ResourceDictionary>
            <converters:VisibilityConverter x:Key="VisibilityConverter" />
于 2012-04-13T06:20:56.117 回答