使用自定义ItemTemplate时ListView StyleTriggers不起作用

时间:2019-05-17 19:14:18

标签: c# wpf xaml

在我的应用程序中,我有一个ListView,并且需要一些Style.Triggers。我使用自定义ItemTemplate,以便在文本旁边有一个图标。奇怪的是,当我不使用ListView.View时,触发器不起作用。

当我(出于调试原因,使用ListView.View时,它工作正常。关于如何绕过此方法的任何建议吗?我尝试在DataTemplate上设置背景,但是我不认为thiw是正确的解决方案,因为我需要更多的触发器,例如IsMouseOver。

触发器不起作用:

<ListView ItemsSource="{Binding SecondaryTabs}"
                          BorderThickness="0"
                          Background="Transparent"
                          ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                          ScrollViewer.CanContentScroll="False"
                          SelectedIndex="{Binding SelectedSecondaryTabIndex}"
                          ItemTemplate="{StaticResource SidePaneListViewTemplate}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonUp">
                            <i:InvokeCommandAction Command="{Binding Path=SelectedChildTabChangedCommand, Mode=OneWay}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <ListView.ItemContainerStyle>
                        <Style TargetType="{x:Type ListViewItem}">
                            <Style.Triggers>
                                <Trigger Property="Control.IsMouseOver"
                                         Value="True">
                                    <Setter Property="Control.Background"
                                            Value="Green" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>

触发器工作:

<ListView ItemsSource="{Binding SecondaryTabs}"
                          BorderThickness="0"
                          Background="Transparent"
                          ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                          ScrollViewer.CanContentScroll="False"
                          SelectedIndex="{Binding SelectedSecondaryTabIndex}"
                          ItemTemplate="{StaticResource SidePaneListViewTemplate}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonUp">
                            <i:InvokeCommandAction Command="{Binding Path=SelectedChildTabChangedCommand, Mode=OneWay}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <ListView.ItemContainerStyle>
                        <Style TargetType="{x:Type ListViewItem}">
                            <Style.Triggers>
                                <Trigger Property="Control.IsMouseOver"
                                         Value="True">
                                    <Setter Property="Control.Background"
                                            Value="Green" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Date"
                                            Width="115"
                                            DisplayMemberBinding="{Binding Date, StringFormat='{}{0:dd/MM/yyyy-HH:mm}'}" />
                            <GridViewColumn Header="Amount"
                                            Width="40"
                                            DisplayMemberBinding="{Binding Amount}" />
                            <GridViewColumn Header="Method"
                                            Width="70" />
                            <GridViewColumn Header="User"
                                            Width="80"
                                            DisplayMemberBinding="{Binding User.UserName}" />

                        </GridView>
                    </ListView.View>
                </ListView>

关于为什么会这样的任何想法?

0 个答案:

没有答案