【问题标题】:WPF Databound Bulleted ListWPF 数据绑定项目符号列表
【发布时间】:2009-05-15 18:54:19
【问题描述】:

如何在 WPF 中创建数据绑定的项目符号超链接列表?

我有这个:

<ItemsControl Name="lstScripts">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Hyperlink>
                    <TextBlock Text="{Binding Path=Name}" />
                </Hyperlink>
            </TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

但我不知道如何将这些物品变成子弹。我看到 BulletDecorator,但我不想指定自己的项目符号图像,我只想要标准项目符号。

【问题讨论】:

    标签: wpf itemscontrol


    【解决方案1】:

    不幸的是,没有“标准项目符号”...这是一个简单的椭圆项目符号示例:

            <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>
    

    【讨论】:

    • 在某种程度上有标准的项目符号。您可以使用 TextBlock 作为 Bullet 来显示 Unicode 项目符号 U+2022。
    【解决方案2】:

    只是为了扩展@Thomas Levesque 的回答,把它变成一个 UserControl 以便你可以重复使用它,例如:

    <Reporting:BulletedItem BulletText="Bullet Item 1" />
    <Reporting:BulletedItem BulletText="Bullet Item 2" />
    

    创建一个用户控件:

    <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>
    

    在代码中:

    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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      对于各种列表,您可以使用 FlowDocument 和 List。它的 MarkerStyle 为“Disc”,其中之一为“Circle”。

      <FlowDocument>
        <List MarkerStyle="Disc">
          <ListItem>
            <Paragraph>Boron</Paragraph>
          </ListItem>
          <ListItem>
            <Paragraph>Carbon</Paragraph>
          </ListItem>
      </<FlowDocument>
      

      这里有更多细节:https://msdn.microsoft.com/library/aa970909(v=vs.100).aspx

      【讨论】:

        猜你喜欢
        • 2014-02-08
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多