开发者

WPF: Change ListBox ItemTemplate depending on whether the ScrollBar is visible or not

I'm sorry for my English.


I need to change the DataTemplate for items in a ListBox depending on whether the Vertical ScrollBar is visible or not (or enabled or disabled). I use styles for ListBox and ScrollBar. I can change scrollBar template when its property "IsEnabled" has value "False". But I can't understand how to catch ScrollBar.VisibilityChanging inside ListBox Style. I tryed to use

<Style TargetType="{x:Type ListBox}" >
..开发者_JAVA技巧...
<Style.Triggers>
    <Trigger Property="ScrollViewer.ComputedVerticalScrollBarVisibility"
             Value="Hidden">
        <Setter Property="ItemTemplate">
            ......

...with...

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

It is not work.

I hope you help me


The ScrollViewer has two properties: ComputedHorizontalScrollBarVisibility and ComputedVerticalScrollBarVisibility that are read-only dependency properties and we can use them in Triggers in the ControlTemplate of our ListBox (here I'm considering only the vertical property)

<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>

NOTE: for clarity of the answer, this is a stripped-down, bare-bones template for a ListBox. I removed the Border that should wrap around the ScrollViewer and all the properties that are defined on the ScrollViewer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜