17

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.

Dave Clemmer
  • 3,757
  • 12
  • 47
  • 72
Christopher
  • 217
  • 2
  • 7

3 Answers3

33

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>
Thomas Levesque
  • 270,447
  • 59
  • 580
  • 726
  • There are standard bullets in a way. You could use a TextBlock as Bullet to show the Unicode bullet U+2022. – Jens Jan 15 '20 at 13:39
2

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;
    }
}
DaveDev
  • 38,095
  • 68
  • 199
  • 359
2

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

user1069816
  • 2,173
  • 2
  • 21
  • 39