1

私はRadDataBoundListBoxを持っています(より多くのオプションやプロパティなどを備えた高度なリストボックスです...)私のListBoxは通知のリストにバインドされています、すべての通知にはboolプロパティUnreadがあります、私がしたいのはアイテムが未読(未読)の場合です= true)フォントの色を青にしたいのですが、未読= 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;

私がこれをどのように達成できるか知っていますか?つまり、通知が[読み取り]の場合、フォントの色は同じままですが、未読の場合はアイテムの色を変更します(通知)。

ロードされたときのリストボックスのスクリーンショットは次のとおりです

リストボックスを表示するには、ここをクリックしてください

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 に答える