【问题标题】:Find element in WPF scrollbar在 WPF 滚动条中查找元素
【发布时间】:2017-02-09 13:46:12
【问题描述】:

在我花了几个小时在 WPF 中设计滚动条后,我遇到了一个问题。整个滚动条在资源字典中定义。我创建了一个垂直滚动条,XAML 代码在这里

<!-- Vertical ScrollBar with grip control definition -->
  <ControlTemplate x:Key="VerticalScrollBarWithGrip" TargetType="{x:Type ScrollBar}">
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition MaxHeight="18"/>
        <RowDefinition MaxHeight="18"/>
        <RowDefinition Height="0.00001*"/>
        <RowDefinition MaxHeight="18"/>
      </Grid.RowDefinitions>

      <!-- GripControl -->
      <Border BorderThickness="1" CornerRadius="1" Name="PART_SplittGripCtrl">
        <Border.Style>
          <Style>
            <Setter Property="Border.Background" Value="{StaticResource ScrollBarLightColor}"/>
            <Setter Property="Border.BorderBrush" Value="#C9C9C9"/>
            <Style.Triggers>
              <Trigger Property="Border.IsMouseOver" Value="True">
                <Setter Property="Border.BorderBrush" Value="{StaticResource BorderHighlight}"/>
                <Setter Property="Border.Background" Value="{StaticResource BorderHighlight}"/>
                <Setter Property="Border.Cursor" Value="SizeNS"/>
              </Trigger>
            </Style.Triggers>
          </Style>
        </Border.Style>
        <Image Source="Res/grip.png" Height="17" Width="17" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
      </Border>

      <Border Grid.Row="1" Grid.RowSpan="3" Background="{StaticResource ScrollBarLightColor}"/>
      <RepeatButton Grid.Row="1" Style="{StaticResource ScrollBarLineButton}" Height="18" Command="ScrollBar.LineUpCommand" Content="M 0 5 L 10 5 L 5 0 Z"/>
      <Track x:Name="PART_Track" Grid.Row="2" IsDirectionReversed="true">
        <Track.DecreaseRepeatButton>
          <RepeatButton Style="{StaticResource ScrollBarPageButton}" Command="ScrollBar.PageUpCommand"/>
        </Track.DecreaseRepeatButton>
        <Track.Thumb>
          <Thumb Style="{StaticResource ScrollBarThumb}" Margin="5,2">
            <Thumb.Background>
              <SolidColorBrush Color="{StaticResource DefaultScrollBarColor}"/>
            </Thumb.Background>
          </Thumb>
        </Track.Thumb>
        <Track.IncreaseRepeatButton>
          <RepeatButton Style="{StaticResource ScrollBarPageButton}" Command="ScrollBar.PageDownCommand"/>
        </Track.IncreaseRepeatButton>
      </Track>
      <RepeatButton Grid.Row="4" Style="{StaticResource ScrollBarLineButton}" Height="18" Command="ScrollBar.LineDownCommand" Content="M 0 0 L 5 5 L 10 0 Z"/>
    </Grid>
  </ControlTemplate>

你可以看到,我添加了一个名为“PART_SplittGripCtrl”的元素。底部的图片向您展示了我的滚动条的样子。

是的,我的滚动条应该具有与 Visual Studio 类似的行为。

现在我想在 C# 代码中找到“PART_SplittGripCtrl”,但我没有找到这个元素。所以我有一些问题。

  1. 如何在滚动条资源中找到 PART_SplittGripCtrl?
  2. 如何将事件添加到在资源中创建的 WPF 元素 字典?
  3. 一时忘记了,想问什么

对于一些提示,我非常感激。

***** 编辑 *****

我现在重命名了一些部分。我的 CustomControl 看起来像这样

[TemplatePart(Name = "PART_SplittGripCtrl", Type =typeof(Border))]
  public class VsScrollbar : ScrollBar
  {
    static VsScrollbar()
    {
      DefaultStyleKeyProperty.OverrideMetadata(typeof(VsScrollbar), new FrameworkPropertyMetadata(typeof(VsScrollbar)));
    }

    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();
    }
  }

Generic.xaml 在这里

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Org.Vs.ScrollbarTest.CustomControls">


  <Style TargetType="{x:Type local:VsScrollbar}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:VsScrollbar}">
          <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

【问题讨论】:

    标签: wpf scrollbar resourcedictionary


    【解决方案1】:

    除了模板中有错字(SplittGripCtrl vs SplitterGripCtrl)之外,在 OnApplyTemplate 中执行此操作应该可以工作

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
    
            var border= this.GetTemplateChild("SplittGripCtrl") as Border;
            if (border!= null)
            {
                border.Background = new SolidColorBrush(Colors.Red);
                border.MouseUp += (sender, args) => MessageBox.Show("hi");
            }
        }
    

    您还应该养成这样声明模板的重要部分的习惯。只是为了允许模板覆盖。

    [TemplatePart(Name = "SplitterGripControl", Type = typeof(Border))]
    

    您只需要创建一个新的用户定义元素。将其更改为从 Scrollbar 继承。然后该控件将包含您的自定义代码类和包含在主题/generic.xaml 中的声明性语法

    【讨论】:

    • 谢谢,但现在我像这样添加我的资源字典&lt;Window.Resources&gt; &lt;ResourceDictionary&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="ScrollbarResources.xaml"/&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;/ResourceDictionary&gt; &lt;/Window.Resources&gt;。我用你的方法阅读了我必须创建一个自己的控件类。我不知道如何为滚动条创建这样的类。
    • @simmeone 添加了截图。
    • 感谢您的提示。我创建了一个继承自 ScrollBar 的 CustomControl。在我的 ScrollbarResources.xaml 中,我添加了对 CustomControl 的引用,如下所示: 。但是现在我从 .NET 得到一个异常:在 PresentationFramework.dll 中发生了“System.ArgumentException”类型的第一次机会异常附加信息:“TargetType 'MyScrollbar'” für“ControlTemplate” stimmt nicht mit dem als Vorlage verwendeten Typ “ScrollBar” überein。什么鬼?
    • 显示您的 generic.xaml 控件使用代码和代码隐藏文件(控件之一)
    • @simmeone 在我看来就像一个空模板 + 用户定义的控制代码文件。移动代码并再次更新您的问题:)
    猜你喜欢
    • 2013-08-08
    • 2022-01-20
    • 2015-10-06
    • 2016-04-04
    • 1970-01-01
    • 2011-06-20
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多