I’ve just recently started working on an app that displays a bunch of xml data in a listBox, but the problem is not every xml item has a child value (In my problem, upload), so its listBox code shouldn’t be displayed.
Example:
<item>
<id>1</id>
<body>Some text</body>
<upload></upload>
<created>Some text</created>
</item>
I’m getting the data and populating my list this way:
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;
And my xaml looks like:
<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>
What I want to do is only display the paragraph holding the upload info if the xml upload child holds a value. Otherwise, just remove it from the listBox. I can’t figure out how to change the basic listBox template though.
Any help would be great!
How about using
TextBlocksto display Body and Created? The middle part would be aRichTextBox, you can use a Visibility converter to toggle hide/show if Upload is empty.You need to include your converter in your xaml page first,
then use it inside your template,
The converter should be something like this,
For more info regarding value converter, see this.
UPDATE
In your App.xaml, do this,