2011-07-29 37 views
2

对不起我的英语。WPF:根据ScrollBar是否可见,更改ListBox ItemTemplate


我需要更改项目的DataTemplate中视垂直滚动条是否可见或不可见(或启用或禁用)列表框。 我使用ListBox和ScrollBar的样式。 当属性“IsEnabled”的值为“False”时,我可以更改scrollBar模板。但我无法理解如何捕捉列表框样式中的ScrollBar.VisibilityChanging。我tryed使用

<Style TargetType="{x:Type ListBox}" > 
..... 
<Style.Triggers> 
    <Trigger Property="ScrollViewer.ComputedVerticalScrollBarVisibility" 
      Value="Hidden"> 
     <Setter Property="ItemTemplate"> 
      ...... 

...与...

<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}"> 
    .......... 
    <Trigger Property="IsEnabled" Value="False"> 
      <Setter Property="Visibility" Value="Hidden" /> 
    </Trigger> 
...... 

这是不行的。

我希望您能帮我

回答

2

的ScrollViewer中有两个属性:ComputedHorizo​​ntalScrollBarVisibility和ComputedVerticalScrollBarVisibility这是只读的依赖属性,我们可以触发我们的ListBox的控件模板(这里我只考虑使用它们垂直属性)

<Style x:Key="StyleListBoxChangingItemTemplate" TargetType="{x:Type ListBox}"> 
    <Setter Property="ItemTemplate" Value="{StaticResource SomeItemTemplate}"/> 
    <Setter Property="Template"> 
     <ControlTemplate TargetType="{x:Type ListBox}"> 
      <ScrollViewer x:Name="ListScroller"> 
       <ItemsPresenter /> 
      </ScrollViewer> 
      <ControlTemplate.Triggers> 
       <Trigger SourceName="ListScroller" Property="ComputedVerticalScrollBarVisibility" Value="Visible"> 
        <Setter Property="ItemTemplate" Value="{StaticResource SomeOtherItemTemplate}"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Setter> 
</Style> 

注意:为了清楚说明答案,这是一个精简的ListBox的简单模板。我删除了应该围绕ScrollViewer和ScrollViewer上定义的所有属性的边框。

+0

谢谢!我不知道属性“ComputedVerticalScrollBarVisibility”必须像这样使用。现在我的列表框就像我想要的那样工作。 – Kirahvi

相关问题