【问题标题】:Make active tab visible in scrollviewer使活动选项卡在滚动查看器中可见
【发布时间】:2014-07-22 12:27:10
【问题描述】:

我的选项卡控件中有很多选项卡,所以我使用了want to make scrollable tabs for a tabcontrol 的解决方案。 问题是在我的窗口中我有按钮上一个 - 下一个,可以更改活动选项卡。所以我想滚动查看器自动移动到活动选项卡。可以使用FrameworkElementBringIntoView() 方法制作。但是我该如何实现呢?

【问题讨论】:

  • 当你尝试使用BringIntoView时发生了什么?
  • @Sheridan 只是不知道如何以及在哪里调用它)

标签: c# wpf tabcontrol


【解决方案1】:

给你,我通过创建附加行为解决了这个问题 (Attached Properties)

如果您的模板与您发布的链接中的相似

添加以下样式,并将标签项的附加属性ScrollHelper.SelectScroll绑定到IsSelected

    <TabControl>
        <TabControl.Resources>
            <Style TargetType="TabItem" xmlns:l="clr-namespace:CSharpWPF">
                <Setter Property="l:ScrollHelper.SelectScroll"
                        Value="{Binding IsSelected,RelativeSource={RelativeSource Self}}" />
            </Style>
        </TabControl.Resources>
        ...
    </TabControl>

行为类

namespace CSharpWPF
{
    class ScrollHelper : DependencyObject
    {
        public static bool GetSelectScroll(DependencyObject obj)
        {
            return (bool)obj.GetValue(SelectScrollProperty);
        }

        public static void SetSelectScroll(DependencyObject obj, bool value)
        {
            obj.SetValue(SelectScrollProperty, value);
        }

        // Using a DependencyProperty as the backing store for SelectScroll.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectScrollProperty =
            DependencyProperty.RegisterAttached("SelectScroll", typeof(bool), typeof(ScrollHelper), new PropertyMetadata(false, OnSelectScroll));


        private static void OnSelectScroll(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TabItem tab = d as TabItem;
            if ((bool)e.NewValue)
            {
                tab.BringIntoView();
            }
        } 
    }
}

更改属性后,它将调用BringIntoView() 方法,该方法会将选项卡拉到视图中,因此scrollviewer 将滚动到选项卡

您可以根据自己的喜好选择重命名属性或类,我只是随机选择了一个我想到的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-18
    • 2018-12-04
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多