当鼠标结束(或选择)特定项目时,Datatrigger listview的ItemContainer不起作用

问题描述 投票:0回答:1

在ListView中,我使用DataTrigger条件将Item(textblock)的背景颜色绑定到Integer属性,因为当属性更改(从0到1)时,我希望该项目的背景从透明变为红色。

此触发器在两种情况下不起作用:当鼠标位于项目上方时,或者选择此特定项目时。在这两种情况下,尽管具有属性值,但背景始终是透明的。

因此,只有当鼠标位于其他不同的项目上或其他项目被选中时,该项目才会更改其背景。

textblock在单独的DataTemplate中定义(它是BoardViewModel的一部分),ListView是这些VM的集合。在我的代码中,我检查了文本块描述中没有“isMouseOver”活动触发条件,并且listview的部分中没有“isSelected”活动触发器。我也试图删除结构的背景,但这对问题没有影响。

--DataTemplate ---

    <DataTemplate DataType="{x:Type viewModels:BoardViewModel}">
        <Grid >

            <Grid HorizontalAlignment="Stretch"  Grid.Row="0" Width="Auto" Height="Auto">
                <Border Margin="20,20,20,20" BorderThickness="2" BorderBrush="Transparent" HorizontalAlignment="Stretch">
                    <TextBlock HorizontalAlignment="Stretch">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="ID: {0}">
                                <Binding Path="ID"/>
                            </MultiBinding>
                        </TextBlock.Text>
        </TextBlock>
                </Border>
            </Grid>

        </Grid>
    </DataTemplate>

---观点---

        <Grid Grid.Row="0">
            <ListView Height="Auto" Background="{StaticResource BoardBackground}" ItemsSource="{Binding Path= BoardViewModels}" SelectedItem="{Binding CurrentBoardViewModel}" IsSynchronizedWithCurrentItem="true">
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">

                        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path= ErrorRedFlagByBoard}" Value="1">
                                <Setter Property="Background" Value="Red"></Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path= ErrorRedFlagByBoard}" Value="0">
                                <Setter Property="Background" Value="Transparent"></Setter>
                            </DataTrigger>

                        </Style.Triggers>
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>
        </Grid>

我检查了viewmodel的整数属性的正确值,所以我预计当项目被选中或者鼠标在项目上时项目的背景仍然是红色,但是这个断言无效,我不明白是什么。谢谢你们。

wpf xaml listview background datatrigger
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.