简体   繁体   中英

DataTrigger WPF

I have two lists. List of Things and List of Fruits. If an item in the list of things is a fruit from the fruits list, I would like that item to be highlighted.

I would like this with data-binding and thru xmal and not code-behind b/c I am doing MVVM pattern. I have tried it with DataTrigger and Converter but can't get it to work. Please help.

Thanks.

    <ListBox ItemsSource="{Binding Things}"
        Name="ListOfThigns"
        Grid.Row="1">
        <DataTemplate>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Fruits}" >
                    <Setter Property="ListBoxItem.Background" Value="Green"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListBox>

    <ListBox ItemsSource="{Binding Fruits}"
        Name="ListOfFruits"
        Grid.Column="1"
        Grid.Row="1">
    </ListBox>

Or, if Fruit inherits from Thing (a fruit is a thing):

    <ListView ItemsSource="{Binding Things}">
        <ListView.Resources>
                <DataTemplate DataType="{x:Type dm:Fruit}">
                    <TextBlock Text="{Binding Name}" Background="RoyalBlue"/>
                </DataTemplate>
                <DataTemplate DataType="{x:Type dm:Thing}">
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
        </ListView.Resources>
    </ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM