开发者

WPF Highlight Item

开发者 https://www.devze.com 2022-12-17 23:04 出处:网络
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.

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="Or开发者_Go百科ange" />
                </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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号