How do I create a databound, bulleted list of hyperlinks in WPF?
I've got this:
<ItemsControl Name="lstScripts">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink>
<TextBlock Text="{Binding Path=Name}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But I can't figure out how to turn the items into bullets. I see the BulletDecorator, but I don't want to specify my own bullet image, I just want standard bullets.
Unfortunately there are no "standard bullets"... Here's an example of a simple Ellipse bullet :
<ItemsControl Name="lstScripts">
<ItemsControl.ItemTemplate>
<DataTemplate>
<BulletDecorator Width="Auto">
<BulletDecorator.Bullet>
<Ellipse Fill="White" Stroke="Black" StrokeThickness="1" Width="8" Height="8"/>
</BulletDecorator.Bullet>
<TextBlock>
<Hyperlink>
<TextBlock Text="{Binding Path=Name}" />
</Hyperlink>
</TextBlock>
</BulletDecorator>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Just to extend @Thomas Levesque's answer a bit, Make it into a UserControl so you can reuse it, e.g.:
<Reporting:BulletedItem BulletText="Bullet Item 1" />
<Reporting:BulletedItem BulletText="Bullet Item 2" />
Create a UserControl:
<UserControl x:Class="MyNameSpace.Reporting.BulletedItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" >
<Grid>
<ItemsControl >
<BulletDecorator Width="Auto" Margin="10, 0, 0, 0">
<BulletDecorator.Bullet>
<Ellipse Fill="Black" Stroke="Black" StrokeThickness="1" Width="5" Height="5"/>
</BulletDecorator.Bullet>
<TextBlock Margin="5, 0, 0, 0">
<TextBlock Text="{Binding BulletText}" />
</TextBlock>
</BulletDecorator>
</ItemsControl>
</Grid>
</UserControl>
In the code:
public partial class BulletedItem : UserControl
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("BulletText", typeof(string), typeof(BulletedItem));
public string BulletText
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public BulletedItem()
{
InitializeComponent();
this.DataContext = this;
}
}
For all sorts of list you can use the FlowDocument and List. This has a MarkerStyle of "Disc" and one of "Circle."
<FlowDocument>
<List MarkerStyle="Disc">
<ListItem>
<Paragraph>Boron</Paragraph>
</ListItem>
<ListItem>
<Paragraph>Carbon</Paragraph>
</ListItem>
</<FlowDocument>
There are more details here: https://msdn.microsoft.com/library/aa970909(v=vs.100).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With