在我花了几个小时在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”,但我找不到这个元素。所以我有一些问题。
对于一些提示,我非常感激。
*****编辑*****
我现在重命名一些部分。我的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>
答案 0 :(得分: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))]
您只需要创建一个新的用户定义元素。将其更改为从滚动条继承。然后,该控件将包含您的自定义代码类和themes / generic.xaml中包含的声明性语法