我刚刚开始研究一个在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中删除即可。我不知道如何更改基本的listBox模板。
任何帮助都会很棒!
答案 0 :(得分:0)
如何使用TextBlocks
显示Body和Created?中间部分为RichTextBox
,如果上传为空,您可以使用可见性转换器来切换隐藏/显示。
您需要首先在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();
}
}
有关值转换器的详细信息,请参阅this。
<强>更新强>
在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" />