views:

80

answers:

1
+1  Q: 

WPF Highlight Item

I have a ViewModel that provides a collection of Items. There is also a ActiveItem propery. The Items collection may or may not contain ActiveItem.

What I want to do (in XAML) is display the items as a list and highlight any of the items that are equal to Active Item.

I have tried the following with no success:

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border x:Name="outerBorder" Background="Green">
                <TextBlock Text="{Binding ItemId}" />
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger 
                    Binding="{Binding      
                        Path=DataContext.Item.ItemId,      
                        RelativeSource={RelativeSource TemplatedParent},     
                        Mode=Default}"
                    Value="{Binding      
                        Path=DataContext.ActiveItem.ItemId,      
                        RelativeSource={RelativeSource AncestorType=Window},     
                        Mode=Default}"
                >
                    <Setter TargetName="outerBorder" 
                        Property="Background" Value="Orange" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

As you can see I attempted to use a DataTrigger to match the current item with the ActiveItem but it doesn't work. I think this is something to do with my trying to use a Binding in DataTrigger.Value - something I haven't seen any other examples of.

Any ideas how I might make this work?

Thanks,

Daniel

+2  A: 

Since you're using MVVM, why not just have the view model expose a property telling the view whether it's active or not? That'll get that logic out of you view and into your VM.

HTH,
Kent

Kent Boogaart
So instead of exposing Items (model object) directly I should create an ItemViewModel and expose that? I would be reluctant to add an IsActive property directly to my Item model object.
Daniel Skinner