WPF ListBox如何将单个项目字体加粗?

5

根据我放入列表框的对象的属性,我希望其中一些项目以粗体显示。

我认为可以通过更改模板来实现,但似乎找不到例子。

谢谢!

2个回答

5
您可以使用DataTriggers来实现此操作。关于如何使用DataTrigger的简单示例,您可以查看http://manicprogrammer.com/cs/blogs/willeke/archive/2007/04/25/datatriggers-are-very-cool.aspx。这里有一个使用ListView的示例,但同样适用于Listbox。
<ListView x:Name="Notes" Margin="4,4,4,4" 
   ItemsSource="{Binding Path=CurrentCustomer.CustomerNotes}"
   ItemTemplate="{DynamicResource CustomerNotesDataTemplate}" 
   Style="{DynamicResource ListViewStyle1}" />

然后,ItemTemplate在我的窗口资源中:

        <DataTemplate x:Key="CustomerNotesDataTemplate">
            <Grid MinWidth="400" Style="{DynamicResource NotesRowTriggers}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="74"/>
                    <ColumnDefinition Width="400"/>
                    <ColumnDefinition Width="125"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Top" Text="{Binding Path=NoteDate, Converter={StaticResource ShortDateConverter}}" Margin="0,0,4,0" />
                <TextBlock Text="{Binding Path=FreeNote}" Grid.Column="1" HorizontalAlignment="Stretch" Margin="4,0,0,0" Grid.ColumnSpan="1" TextWrapping="Wrap" />
                <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding Path=NoteUser}" Grid.Column="2" Width="110" d:LayoutOverrides="Width" Margin="4,0,0,0" Grid.ColumnSpan="1" />
                <CheckBox HorizontalAlignment="Left" IsChecked="{Binding Path=Highlight}" VerticalAlignment="Top" Content="Important" Grid.Column="3" Margin="4,0,4,0"/>
                <CheckBox HorizontalAlignment="Left" IsChecked="{Binding Path=Important}" VerticalAlignment="Top" Content="Alert" Grid.Column="4" Margin="4,0,4,0"/>
            </Grid>
       </DataTemplate>

最后,带有DataTriggers的样式也在我的窗口资源中。

<Style x:Key="NotesRowTriggers" TargetType="{x:Type Grid}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Important}" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="Red" Opacity="0.3" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=Highlight}" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="Red" Opacity="0.6" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

我的例子可能比必要的内容还要冗长,但我只是从我的一个应用程序中直接提取了它。


4

如果您使用转换器(例如IntToFontWeightConverter),则可以更简单地完成此操作。

设置项目模板:

 <DataTemplate x:Key="BoldTemplate">
    <TextBlock
        FontWeight="{Binding Path=Position, Converter={StaticResource IntToFontWeightConverter}}"
        Text="{Binding Path=Name}" 
        />
</DataTemplate>     

在这里,Name是您想要显示的内容,Position是您基于其进行加粗/正常显示的属性。

根据您所选择的属性类型创建转换器。

class IntToFontWeightConverter :IValueConverter 
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((int)value == 1)
        {
            return FontWeights.Bold;
        }

        return FontWeights.Normal;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接