根据项条件的Windows Phone样式项 - XAML样式

时间:2012-04-23 12:41:46

标签: c# wpf silverlight windows-phone-7 xaml

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

这是我的列表框代码:

<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的截图

click here to show my listbox

1 个答案:

答案 0 :(得分: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" />

如果您不熟悉转换器,请查看此处 - &gt; http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=vs.95).aspx