WPF Listview滚动条可见性触发器

时间:2018-12-18 19:03:20

标签: wpf triggers

我正在尝试设置一个触发器,以在隐藏滚动条时删除列表视图上的填充。

我将触发器置于listview样式上,但结果不一致。例如,无论滚动条的可见性如何,触发器中的background属性始终处于活动状态。

我已经查看了ScrollViewer.ComputedVerticalScrollBarVisibility Property 的MSDN,但运气不好找出问题所在。

<ListView Grid.Row="1" Grid.Column="1" BorderBrush="{x:Null}" BorderThickness="0" ItemsSource="{Binding Tasks}" Margin="5"
          ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <!-- SNIP -->
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.Style>
        <Style>
            <Setter Property="ListView.Padding" Value="0,0,5,0"/>
            <Setter Property="ListView.Background" Value="{x:Null}" />
            <Style.Triggers>
                <Trigger Property="ScrollViewer.ComputedVerticalScrollBarVisibility" Value="Hidden">
                    <Setter Property="ListView.Padding" Value="0"/>
                    <Setter Property="ListView.Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.Style>
</ListView>

1 个答案:

答案 0 :(得分:1)

我写了一个对我有用的ListView样式示例。我试过了。

<Style TargetType="ListView">
      <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
      <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
      <Setter Property="Template">
           <Setter.Value>
                  <ControlTemplate TargetType="ListView">
                          <ScrollViewer>
                                 <ScrollViewer.Style>
                                    <Style TargetType="ScrollViewer">
                                       <Style.Triggers>
                                             <Trigger Property="ComputedVerticalScrollBarVisibility" Value="Visible">
                                                <Setter Property="Padding" Value="100"/>
                                              </Trigger>
                                              <Trigger Property="ComputedVerticalScrollBarVisibility" Value="Collapsed">
                                                  <Setter Property="Padding" Value="10"/>
                                              </Trigger>
                                       </Style.Triggers>
                                     </Style>
                                 </ScrollViewer.Style>
                             <ItemsPresenter />
                        </ScrollViewer>
                 </ControlTemplate>
            </Setter.Value>
        </Setter>
 </Style>