problemas para cambiar la propiedad de un elemento en un cuadro de lista

I've created a listbox, to which i can add and delete items dynamically UI changes accordingly and it works fine.

<ListBox Name="MsgsList" ItemsSource="{Binding Items}" Style="{StaticResource MsgsBoxStyle}">
        <DataTemplate x:Name="MsgsDataTemplate">
            <StackPanel Tag="{Binding MsgTagInfo}" ManipulationCompleted="StackPanel_Msgs_ManipulationCompleted">

                    <toolkit:GestureListener Hold="GestureListener_Hold" Tap="GestureListener_Tap"/>

                <Grid x:Name="ContentPanelInner" Grid.Row="1" Width="500">
                    <StackPanel x:Name="stackPanelInner" Width="500">

                        <Grid VerticalAlignment="Top" Width="500">
                                <ColumnDefinition />
                                <ColumnDefinition />

                            <TextBlock Grid.Column="0" Text="{Binding MsgTitle}" Style="{StaticResource MsgLine1}"  />
                            <TextBlock Grid.Column="1" Text="{Binding MsgDate}" Style="{StaticResource MsgDate}" />
                        <TextBlock Text="{Binding MsgBody}" Style="{StaticResource MsgLine2}" />

but i didn't understand how to change a style for a particular item's textblock, say based on some condition, if i want to change color of specific item's textbox(s), not sure how to access that.

can somebody please help me with this? thanks.

preguntado el 01 de febrero de 12 a las 14:02

2 Respuestas

Probably not the easiest way to do this, but arguably the cleanest from a separation of concerns point of view is by using a converter, and binding that to the property you want to monitor...

For example, if your model is changing state based on a boolean property called myProperty, you could use something like this.

<StackPanel Background={Binding myProperty, Converter={StaticResource myBindingConverter}" />

Your converter should return a SolidColorBrush, based on the value of your property.

public class AlternateRowColour : IValueConverter
    SolidColorBrush normal = new SolidColorBrush(Colors.Transparent);
    SolidColorBrush highlighted = new SolidColorBrush(Color.FromArgb(255, 241, 241, 241));

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        var myValue = (bool)value
        return myValue ? highlighted : normal ;

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

Respondido 01 Feb 12, 18:02

Sorry, i didn't understand. How can i change a particular item with this approach, can you please elaborate? - rplusg

If your backing model has a boolean property called myProperty, the converter will return a different brush depending on whether the value is true or false. Other than setting the flag in your model you shouldn't have to manually do any work to check the value. - ZombieOvejas

Thanks for your inputs, but being a lazy developer, i would like to take colin's answer, but gave you an upvote. - rplusg

@rplusg - Fair enough - whatever works for you. I'd recommend revisiting this answer in future if you start down an MVVM path with your apps. :) - ZombieOvejas

I If you just want to change on aspect of an items style, for example its color, you could expose that as property of the model object you are binding. For example, add a property TextColor and bind it as follows:

<TextBlock Text="{Binding MsgBody}" Style="{StaticResource MsgLine2}">
    <SolidColorBrush Color="{Binding TextColor}"/>

This will take precedence over the colour defined via the style.

Respondido 01 Feb 12, 18:02

This is how I would do it for a very simple example, but I would tend to use a converter for anything non-trivial since it helps decouple the models from the view. - ZombieOvejas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.