如何在WPF中为ListBoxItem加下划线?

时间:2009-01-31 23:00:02

标签: wpf formatting listboxitem

如何在WPF中为ListBoxItem加下划线?我正在使用以下内容,但未显示下划线。

<DataTemplate x:Key="Phrase_List">
    <ListBoxItem IsSelected="{Binding IsDefault}">
        <TextBlock Text="{Binding Path=Phrase}" Tag="{Binding Path=ID}" TextDecorations="Underline"  />
    </ListBoxItem>
</DataTemplate>

4 个答案:

答案 0 :(得分:1)

我不知道您尝试使用的代码。请尝试完成您的问题。我使用以下代码在我的小ListBox中为“World”项加下划线。

    <ListBox>
        <ListBoxItem>Hello</ListBoxItem>
        <ListBoxItem>
            <Underline>World</Underline>
        </ListBoxItem>
    </ListBox>

答案 1 :(得分:0)

您可以使用文本块,并将textdecorations属性设置为下划线。请记住,ListBoxItem的内容可以是除文本之外的其他内容,因此为什么它不是在ListBoxItem上设置某些属性的简单情况。

答案 2 :(得分:0)

您需要创建一个项目模板,在TextBlock控件中显示您的文本。在TextBlock上,将TextDecorations属性(这是一个集合)设置为包含“下划线”。

答案 3 :(得分:0)

在XAML中:

<ListBox Name="lst">
      <ListBoxItem Content="item1" />
      <ListBoxItem Content="item2" FontStyle="Italic" FontWeight="Normal" />
</ListBox>

在C#中:

lst.Items.Clear();
lst.Items.Add(new ListBoxItem { Content = "item 1" });
lst.Items.Add(new ListBoxItem { Content = "item 2" });
lst.Items.Add(new ListBoxItem { Content = "item 3" });

ListBoxItem l = (ListBoxItem)lstItems.Items[2];
li.SetValue(TextElement.FontStyleProperty, FontStyles.Italic);