【发布时间】: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”,但我没有找到这个元素。所以我有一些问题。
- 如何在滚动条资源中找到 PART_SplittGripCtrl?
- 如何将事件添加到在资源中创建的 WPF 元素 字典?
- 一时忘记了,想问什么
对于一些提示,我非常感激。
***** 编辑 *****
我现在重命名了一些部分。我的 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