WPF将触发器应用于listview项目以更改背景

时间:2017-06-18 21:15:14

标签: wpf xaml listboxitem

我正在尝试在桌面应用程序中创建一种用于导航的左侧菜单。我的想法是使用按钮作为Listview项目应该以这种方式行事:当我用鼠标悬停时,它们背景应该改变颜色(变成黑暗),当我点击一个时,它的背景颜色应该持续改变(到darkCyan)直到我点击在列表的另一个按钮上。问题是我使用DataTemplate属性来指定按钮的外观,我试着应用触发器来更改ListView的ControlTemplate上的背景颜色。结果是,有时背景颜色会改变,但与按钮相关的命令相反不会被触发。我认为我在树视图的错误元素中做了一些事情,但是我对树视图没有足够的了解,所以我不明白我做错了什么。这是XAML的代码,我在其中定义了Buttons和ListView的样式

<Window.Resources>
    <DataTemplate DataType="{x:Type viewModels:TripViewModel}">
        <views:TripView />
    </DataTemplate>

    <Style TargetType="{x:Type Button}">
        <Setter Property="Height"
                Value="50" />
        <Setter Property="Background"
                Value="#555D6F" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border>
                        <Grid Background="Transparent">
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border BorderBrush="Transparent"
                            BorderThickness="0"
                            Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver"
                                 Value="True">
                            <Setter Property="Background"
                                    Value="DarkCyan" />
                        </Trigger>
                        <Trigger Property="IsSelected"
                                 Value="True">
                            <Setter Property="Background"
                                    Value="DarkCyan" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

这是我创建ListView的代码

<ListView Name="MenuButtons"
      ItemsSource="{Binding PageViewModels}"
      Background="Transparent"
      IsSynchronizedWithCurrentItem="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding Name}"
                    Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                    Margin="0" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

任何人都可以提供帮助? 感谢

1 个答案:

答案 0 :(得分:1)

我已经通过使用ListBox而不是ListView解决了这个问题,并通过以下方式将ItemContainer设置为按钮

<ListBox.ItemTemplate>
    <ItemContainerTemplate>
        <Button Content="{Binding Name}"
                Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                Margin="0" />
    </ItemContainerTemplate>
</ListBox.ItemTemplate>
相关问题