1

我有一个 RadDataBoundListBox (它是一个具有更多选项和属性等的高级列表框......)我的 ListBox 绑定到一个通知列表,每个通知都有一个布尔属性未读​​,我想要做的是如果项目未读(未读= true) 我希望字体颜色为蓝色,如果 Unread = false 字体颜色保持白色...我不知道该怎么做

这是我的列表框代码:

<telerikPrimitives:RadDataBoundListBox 
        HorizontalAlignment="Left" 
        Margin="3,4,0,0" 
        Name="radListNotifications" 
        VerticalAlignment="Top" 
        Width="453" 
        SelectionChanged="radListNotifications_SelectionChanged">
    <telerikPrimitives:RadDataBoundListBox.ItemTemplate>
        <DataTemplate>                                                 
            <StackPanel>
                <TextBlock  Text="{Binding Message}" 
                            TextWrapping="Wrap" 
                            HorizontalAlignment="Left" 
                            VerticalAlignment="Top" 
                            Padding="0,3,0,0" 
                            FontFamily="{StaticResource PhoneFontFamilyLight}" 
                            FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
                            Foreground="{StaticResource PhoneForegroundBrush}" 
                            LineStackingStrategy="BlockLineHeight" 
                            LineHeight="43" />

                <TextBlock  Text="{Binding Time}" 
                            Opacity="0.65" 
                            HorizontalAlignment="Left" 
                            FontSize="{StaticResource PhoneFontSizeSmall}" 
                            TextWrapping="Wrap" 
                            VerticalAlignment="Top" 
                            Foreground="{StaticResource PhoneForegroundBrush}" 
                            FontFamily="{StaticResource PhoneFontFamilyNormal}" 
                            Margin="0,0,0,13"/>
            </StackPanel>                              
        </DataTemplate>
    </telerikPrimitives:RadDataBoundListBox.ItemTemplate>
</telerikPrimitives:RadDataBoundListBox>

这是我应用 ItemSource 的代码:

List<TimeTierUserActions.Notification> listNotifications = e.Result.ToList();
radListNotifications.ItemsSource = listNotifications;

你知道我怎么能做到这一点吗?我的意思是如果通知是 Read 字体颜色保持不变,但如果未读更改项目的颜色(通知)。

这是加载时 ListBox 的屏幕截图

单击此处显示我的列表框

4

1 回答 1

2

假设您的对象上有一个属性表示消息是否未读,您应该使用转换器来确定前景色...

<TextBlock 
    Text="{Binding Message}" 
    TextWrapping="Wrap" 
    HorizontalAlignment="Left" 
    VerticalAlignment="Top" 
    Padding="0,3,0,0" 
    FontFamily="{StaticResource PhoneFontFamilyLight}" 
    FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
    Foreground="{Binding IsUnread, Converter={StaticResource UnreadBrushConverter}}" 
    LineStackingStrategy="BlockLineHeight" 
    LineHeight="43" />

然后,您的转换器会将未读标志作为参数,就像这样......

public class UnreadMessageBrushconverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        // hard-coded colours example,  you may want to look at 
        // using predefined resources for this, though.
        return (bool)value ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.White);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

您还需要在 XAML 中声明转换器的实例(在本例中使用命名空间“conv”)

<conv:UnreadMessageBrushconverter x:Key="UnreadBrushConverter" />

如果您不熟悉转换器,请看这里 -> http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=vs.95).aspx

于 2012-04-23T12:49:52.287 回答