滚动查看器在wpf中调整大小(右下角)

时间:2016-04-19 13:21:54

标签: wpf telerik wpf-controls

我在树视图和列表框中启用了滚动查看器,甚至自定义了引用this网站的滚动条,我已经实现了我需要的功能。我的滚动条现在看起来如下所示

enter image description here

但我需要我的滚动条看起来像这样

enter image description here

我需要右下角的空间用水平或垂直滚动​​条填充。是否有可能在wpf ??

以下是滚动条的自定义样式

<local:ThicknessConverter x:Key="ThicknessConverter" />
    <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Style.Triggers>
            <Trigger Property="Orientation" Value="Horizontal">
                <Setter Property="Width" Value="Auto"/>
                <Setter Property="Height" Value="18" />
                <Setter Property="Template"

                    Value="{StaticResource HorizontalScrollBar}" />
            </Trigger>
            <Trigger Property="Orientation" Value="Vertical">
                <Setter Property="Width" Value="18"/>
                <Setter Property="Height" Value="Auto" />
                <Setter Property="Template"

                    Value="{StaticResource VerticalScrollBar}" />
            </Trigger>
            <Trigger Property="Name" Value="PART_VerticalScrollBar">
                <Setter Property="Margin" Value="{Binding RelativeSource={RelativeSource AncestorType=ScrollViewer},Converter={StaticResource ThicknessConverter}}">
                </Setter>
            </Trigger>

        </Style.Triggers>


    </Style>

这里有树视图代码

   <telerik:RadTreeView x:Name="radTreeView"   Background="#4E4E4E" Margin="0,0,456,0" Grid.Row="2"  
              ItemsSource="{x:Static local:MainWindow.AnimalCategories}" ItemPrepared="treeView_ItemPrepared" 
                             ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" Grid.RowSpan="2" Grid.ColumnSpan="2">
            <telerik:RadTreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Animals}">
                    <TextBlock  Text="{Binding Category}" />
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>

                            <TextBlock Text="{Binding Name}"/>

                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </telerik:RadTreeView.ItemTemplate>

        </telerik:RadTreeView>

1 个答案:

答案 0 :(得分:4)

以下是一种方法:

<强> XAML:

 <ScrollViewer Height="400" Width="400" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" >
    <ScrollViewer.Resources>
        <local:ThicknessConverter x:Key="ThicknessConverter" />
        <Style TargetType="ScrollBar">
            <Style.Triggers>
                <Trigger Property="Orientation" Value="Horizontal">
                    <Setter Property="Margin" Value="{Binding RelativeSource={RelativeSource AncestorType=ScrollViewer},Converter={StaticResource ThicknessConverter}}">
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ScrollViewer.Resources>
</ScrollViewer>

<强> 转换器:

public class ThicknessConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var scrollBars = FindVisualChildren<ScrollBar>(value as DependencyObject);
        foreach (var scrollBar in scrollBars)
        {
            if (scrollBar.Orientation == Orientation.Horizontal)
            {
              return  new Thickness(0, 0, 0, 0 - scrollBar.ActualHeight);
            }
        }
        return new Thickness(0, 0, 0, 0);
    }

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }        

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

<强> 输出:

Scroll