Question

I have a ListBox which contains objects describing a person. This objects are only partially filled and are fully filled (time-costly operation) when the item is clicked. I want to alter the style of those objects which have not yet been filled so that partially-filled items are displayed in italics.

I made an ItemTemplate:

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding}" Style="{StaticResource PersonWithData}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

and defined the Style:

    <Style x:Key="PersonWithData" TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding HasData}" Value="True">
                <Setter Property="FontStyle" Value="Normal"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding HasData}" Value="False">
                <Setter Property="FontStyle" Value="Italic"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

It actually works. The probelm I'm having is to get the style to update when an item is clicked. The object gets filled and should then cause the style to change from italic to normal.

What can I do to force the ListItem or the ListBox to redraw itself and update the styles? Or should I be using some kind of notification when the HasData property changes?

Was it helpful?

Solution

You should make sure that you implement the INotifyPropertyChanged interface in your Person class and emit the change event with a "HasData" parameter every time your HasData property is changed. A good place for this is in the setter of the property. This is key to having bound WPF components update themselves.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top