Update Layout in Listbox...

Oct 23 2008 11:03 AM
Hi...

I have a ListBox control where each entry consists of a template with a stackpanel and a textblock. The listbox is binded to an collection of class "Media". This object has a property "Highlighted". The template for each entry has a datatrigger on the Binding (Binding="{Binding Mode=OneWay, Path=Highlighted}" Value="True"). Then the Foreground Color should be set to "Green": <Setter TargetName="tbTitle" Property="Foreground" Value="Green" />

In the codebehind i registered an eventhandler: OnPreviewKeyDown(KeyEventArgs e). So if i hit the SPACE key the underlying "Media" objects "Highlighted" property will be set to "true".

My problem is now: The foreground color stays the same as before...nothing changed, not before i focused the next item with "Key Down" or "Key Up"...
How can I force the listbox to repaint everything? Is this the right way? Or is there a better method?

...
<DataTemplate x:Key="selectedMenuTemplate">
    <Canvas>
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Left" Orientation="Horizontal">
            <TextBlock Name="tbTitle" Width="40" Text="{Binding Mode=OneWay, Path=Title}" />
        </StackPanel>
    </Canvas>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Mode=OneWay, Path=Highlighted}" Value="True">           
            <Setter TargetName="tbTitle" Property="Foreground" Value="Green" />
       </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>
...

...
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
    Media me = ListBox.SelectedItem as Media;
   
    if (me.Highlighted == true)
        me.Highlighted = false;
    else
        me.Highlighted = true;
       
    e.Handled = true;
}
...