0
在花了幾個小時在WPF中設計滾動條後,我遇到了問題。整個滾動條在資源字典中定義。我創建了一個垂直滾動條,在XAML代碼是在這裏在WPF滾動條中查找元素
<!-- 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>
謝謝你,但此刻我加我的資源字典這樣' ResourceDictionary> '。我用你的方法閱讀,我必須創建一個自己的控制類。我不知道如何爲滾動條創建這樣的類。 –
simmeone
@simmeone添加了截圖。 – Dbl
謝謝你的提示。我創建了一個從ScrollBar繼承的CustomControl。在我的ScrollbarResources.xaml中,我添加了一個對我的CustomControl的引用,如下所示:>。但是現在我從.NET中得到了一個異常:在PresentationFramework中發生了第一次偶然的類型'System.ArgumentException'。dll 附加信息:「TargetType'MyScrollbar'」f「ControlTemplate」stimmt nicht mit dem als Vorlage verwendeten Typ「ScrollBar」überein。 WTF? –
simmeone