Wpf 如何使我的风格影响除其中一个之外的所有滚动浏览者
我有一个ScrollViewer样式,可以更改我应用程序中每个滚动条的外观:Wpf 如何使我的风格影响除其中一个之外的所有滚动浏览者,wpf,visual-studio,xaml,Wpf,Visual Studio,Xaml,我有一个ScrollViewer样式,可以更改我应用程序中每个滚动条的外观: <Style x:Key="{x:Type ScrollViewer}" TargetType="{x:Type ScrollViewer}"> <Setter Property="Tag" Value="Silver"/> <Setter Property="HorizontalScrollBarVisibility" Value="
<Style x:Key="{x:Type ScrollViewer}" TargetType="{x:Type ScrollViewer}">
<Setter Property="Tag" Value="Silver"/>
<Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/>
<Setter Property="VerticalScrollBarVisibility" Value="Hidden"/>
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollContentPresenter
x:Name="PART_ScrollContentPresenter"
Grid.ColumnSpan="2"
Grid.RowSpan="2"
Margin="{TemplateBinding Padding}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
CanContentScroll="{TemplateBinding CanContentScroll}"/>
<ScrollBar
Name="PART_VerticalScrollBar"
Grid.Column="1"
Value="{TemplateBinding VerticalOffset}"
Maximum="{TemplateBinding ScrollableHeight}"
ViewportSize="{TemplateBinding ViewportHeight}"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />
<ScrollBar
Name="PART_HorizontalScrollBar"
Orientation="Horizontal"
Grid.Row="1"
Value="{TemplateBinding HorizontalOffset}"
Maximum="{TemplateBinding ScrollableWidth}"
ViewportSize="{TemplateBinding ViewportWidth}"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="HorizontalScrollBarVisibility"
Value="Auto"/>
<Setter Property="VerticalScrollBarVisibility"
Value="Auto"/>
</Trigger>
</Style.Triggers>
</Style>
<!--Scrollbar Thumbs-->
<Style x:Key="ScrollThumbs" TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Grid x:Name="Grid">
<Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Fill="Transparent" />
<Border x:Name="Rectangle1" CornerRadius="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Background="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ScrollBar}}}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Tag" Value="Horizontal">
<Setter TargetName="Rectangle1" Property="Width" Value="Auto" />
<Setter TargetName="Rectangle1" Property="Height" Value="7" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--ScrollBars-->
<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<Setter Property="Stylus.IsFlicksEnabled" Value="false" />
<Setter Property="Foreground" Value="{Binding Path=Tag, RelativeSource={RelativeSource AncestorType={x:Type ScrollViewer}},Converter={StaticResource StringToColorConverter}}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Width" Value="8" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="GridRoot" Width="8" Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="0.00001*" />
</Grid.RowDefinitions>
<Track x:Name="PART_Track" Grid.Row="0" IsDirectionReversed="true" Focusable="false">
<Track.Thumb>
<Thumb x:Name="Thumb" Background="{TemplateBinding Foreground}" Style="{DynamicResource ScrollThumbs}" />
</Track.Thumb>
<Track.IncreaseRepeatButton>
<RepeatButton x:Name="PageUp" Command="ScrollBar.PageDownCommand" Opacity="0" Focusable="false" />
</Track.IncreaseRepeatButton>
<Track.DecreaseRepeatButton>
<RepeatButton x:Name="PageDown" Command="ScrollBar.PageUpCommand" Opacity="0" Focusable="false" />
</Track.DecreaseRepeatButton>
</Track>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="Thumb" Property="IsMouseOver" Value="true">
<Setter Value="{DynamicResource ButtonSelectBrush}" TargetName="Thumb" Property="Background" />
</Trigger>
<Trigger SourceName="Thumb" Property="IsDragging" Value="true">
<Setter Value="{DynamicResource DarkBrush}" TargetName="Thumb" Property="Background" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Thumb" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="Orientation" Value="Horizontal">
<Setter TargetName="GridRoot" Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="-90" />
</Setter.Value>
</Setter>
<Setter TargetName="PART_Track" Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="-90" />
</Setter.Value>
</Setter>
<Setter Property="Width" Value="Auto" />
<Setter Property="Height" Value="8" />
<Setter TargetName="Thumb" Property="Tag" Value="Horizontal" />
<Setter TargetName="PageDown" Property="Command" Value="ScrollBar.PageLeftCommand" />
<Setter TargetName="PageUp" Property="Command" Value="ScrollBar.PageRightCommand" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这样的样式可以影响每个控件吗?或者我必须为这个样式设置一个名称并使用它
我有一个costment控件,其中包含一个列表框,我希望我的所有容器都受ScrollViewer样式的影响,但这一个除外。我该怎么做
这是我的控件中包含listBox的一部分。这是一份汉堡包菜单:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HamburgerMenu">
<Style TargetType="local:HamburgerMenu">
<Style.Resources>
<!-- Fill Brushes -->
<SolidColorBrush x:Key="DarkBrush" Color="#40000000" />
<SolidColorBrush x:Key="PressedBrush" Color="#80FFFFFF" />
<SolidColorBrush x:Key="PressedBorderBrush" Color="Transparent" />
</Style.Resources>
<Setter Property="Width" Value="50"/>
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="IsOpen" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:HamburgerMenu">
<ControlTemplate.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/HamburgerMenu;component/Themes/ListBoxStyle.xaml"/>
<ResourceDictionary Source="/HamburgerMenu;component/Themes/ToggleButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="local:HamburgerMenuItem">
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=MenuItemForeground}"/>
<Setter Property="SelectionIndicatorColor" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=SelectionIndicatorColor}"/>
<Setter Property="MouseOverColor" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=MouseOverColor}"/>
<Setter Property="PressedColor" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=PressedColor}"/>
<Setter Property="PressedBorderColor" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=PressedBorderColor}"/>
<Setter Property="Data" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=Data}"/>
</Style>
</ResourceDictionary>
</ControlTemplate.Resources>
<Grid x:Name="mainGrid" Background="{TemplateBinding Background}">
<Border x:Name="border" Background="{TemplateBinding Background}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="50" >
<ToggleButton ToolTip="{TemplateBinding HamburIconTooltipMinimizeMessage}" x:Name="menuIcon" Background="Red" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="50" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=IsOpen}"> <!-- hi -->
<StackPanel>
<Path Margin="0,0,0,0" x:Name="path" HorizontalAlignment="Center" VerticalAlignment="Bottom" Stretch="Uniform" Width="30" Fill="{TemplateBinding MenuIconColor}"
Data="M424,135H89c-4.418,0-8-3.582-8-8s3.582-8,8-8h335c4.418,0,8,3.582,8,8S428.418,135,424,135z">
<Path.RenderTransform>
<RotateTransform x:Name="MyAnimatedScaleTransform1" CenterX="0" CenterY="0" Angle="0" />
</Path.RenderTransform>
</Path>
<Path Margin="0,9,0,9" x:Name="path1" HorizontalAlignment="Center" VerticalAlignment="Bottom" Stretch="Uniform" Width="30" Fill="{TemplateBinding MenuIconColor}"
Data="M424,135H89c-4.418,0-8-3.582-8-8s3.582-8,8-8h335c4.418,0,8,3.582,8,8S428.418,135,424,135z">
<Path.RenderTransform>
<MatrixTransform x:Name="myMatrixTransform" >
<MatrixTransform.Matrix >
<Matrix OffsetX="0" OffsetY="0" M11="1" M12="0" />
</MatrixTransform.Matrix>
</MatrixTransform>
</Path.RenderTransform>
</Path>
<Path Margin="0,0,0,0" x:Name="path2" HorizontalAlignment="Left" VerticalAlignment="top" Stretch="Uniform" Width="30" Fill="{TemplateBinding MenuIconColor}"
Data="M424,135H89c-4.418,0-8-3.582-8-8s3.582-8,8-8h335c4.418,0,8,3.582,8,8S428.418,135,424,135z">
<Path.RenderTransform>
<RotateTransform x:Name="MyAnimatedScaleTransform" CenterX="1" CenterY="1" Angle="0" />
</Path.RenderTransform>
</Path>
</StackPanel>
</ToggleButton>
</Border>
<ListBox x:Name="listbox" Style="{StaticResource listboxStyle}" ItemsSource="{TemplateBinding Content}" HorizontalAlignment="Left" Margin="0,40,0,0" VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectedIndex="0"/>
</Grid>
<ControlTemplate.Triggers >
<Trigger Property="IsOpen" Value="False">
<Setter TargetName="menuIcon" Property="ToolTip" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=HamburIconTooltipMaximizeMessage}"/>
<Trigger.EnterActions>
<!-- hide HamburgerIcon ************************************************************************************************************************************* -->
<BeginStoryboard>
<Storyboard>
<MatrixAnimationUsingPath Storyboard.TargetName="myMatrixTransform"
Storyboard.TargetProperty="Matrix" DoesRotateWithTangent="True"
Duration="0:0:0.3" BeginTime="0:0:0.3" >
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M -35 0 C 0 0, 0 0, 5 0"/>
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
<MatrixAnimationUsingPath Storyboard.TargetName="myMatrixTransform"
Storyboard.TargetProperty="Matrix" DoesRotateWithTangent="False"
Duration="0:0:0.5" BeginTime="0:0:0.6" >
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 5 0 C 0 0, 0 0, 0 0"/>
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
<DoubleAnimation
Storyboard.TargetName="path1"
Storyboard.TargetProperty="Opacity"
To="1" BeginTime="0:0:0.3" Duration="00:00:00" AccelerationRatio="0.2" DecelerationRatio="0.8"/>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="MyAnimatedScaleTransform"
Storyboard.TargetProperty="(RotateTransform.Angle)"
Duration="0:0:0.9">
<LinearDoubleKeyFrame Value="-55" KeyTime="0:0:0.1" />
<LinearDoubleKeyFrame Value="-45" KeyTime="0:0:0.3" />
<LinearDoubleKeyFrame Value="10" KeyTime="0:0:0.7" />
<LinearDoubleKeyFrame Value="0" KeyTime="0:0:0.9" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="MyAnimatedScaleTransform1"
Storyboard.TargetProperty="(RotateTransform.Angle)" Duration="0:0:0.9">
<LinearDoubleKeyFrame Value="55" KeyTime="0:0:0.1" />
<LinearDoubleKeyFrame Value="45" KeyTime="0:0:0.3" />
<LinearDoubleKeyFrame Value="-10" KeyTime="0:0:0.7" />
<LinearDoubleKeyFrame Value="0" KeyTime="0:0:0.9" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<!-- Show HamburgerIcon ************************************************************************************************************************************* -->
<MatrixAnimationUsingPath Storyboard.TargetName="myMatrixTransform"
Storyboard.TargetProperty="Matrix" DoesRotateWithTangent="False"
Duration="0:0:0.5" BeginTime="0:0:0.1" >
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 0 0 C 0 0, 0 0, -35 0"/>
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
<DoubleAnimation
Storyboard.TargetName="path1"
Storyboard.TargetProperty="Opacity"
To="0" Duration="00:00:0.1" AccelerationRatio="0.2" DecelerationRatio="0.8" BeginTime="00:00:0.5" />
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="MyAnimatedScaleTransform"
Storyboard.TargetProperty="(RotateTransform.Angle)"
Duration="0:0:0.8" >
<LinearDoubleKeyFrame Value="10" KeyTime="0:0:0.1" />
<LinearDoubleKeyFrame Value="-45" KeyTime="0:0:0.4" />
<LinearDoubleKeyFrame Value="-55" KeyTime="0:0:0.5" />
<LinearDoubleKeyFrame Value="-45" KeyTime="0:0:0.6" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="MyAnimatedScaleTransform1"
Storyboard.TargetProperty="(RotateTransform.Angle)"
Duration="0:0:0.8" >
<LinearDoubleKeyFrame Value="-10" KeyTime="0:0:0.1" />
<LinearDoubleKeyFrame Value="45" KeyTime="0:0:0.4" />
<LinearDoubleKeyFrame Value="55" KeyTime="0:0:0.5" />
<LinearDoubleKeyFrame Value="45" KeyTime="0:0:0.6" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger SourceName="menuIcon" Property="IsMouseOver" Value="true">
<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=MouseOverColor}"/>
</Trigger>
<Trigger SourceName="menuIcon" Property="IsPressed" Value="true">
<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=PressedColor}" />
<Setter TargetName="border" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=PressedBorderColor}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsOpen" Value="True">
<Setter Property="ScrollBarVisibility" Value="Auto"/>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Width"
To="330"
Duration="0:0:0.3" AccelerationRatio="0.1" DecelerationRatio="0.9"/>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Width"
To="300"
Duration="0:0:0.2" BeginTime="0:0:0.3" AccelerationRatio="0.1" DecelerationRatio="0.9"/>
</Storyboard>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Width"
To="50"
Duration="0:0:0.3" AccelerationRatio="0.1" DecelerationRatio="0.9"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
<Style TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource {x:Type ScrollViewer}}">
<Setter Property="Tag" Value="Silver"/>
<Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/>
__YOUR_OTHER_KEYS__
</Style>
<Style x:Key="DefaultScrollViewerStyle" TargetType="{x:Type ScrollViewer}">
</Style>
<Style x:Key="{x:Type ScrollViewer}" TargetType="{x:Type ScrollViewer}">
<Setter Property="Background" Value="Lavender" />
</Style>
<!-- The first one will be lavender. The second won't. -->
<StackPanel Orientation="Vertical">
<ScrollViewer Height="20" />
<ScrollViewer Height="20" Style="{StaticResource DefaultScrollViewerStyle}" />
</StackPanel>
<Style TargetType="ListBox" x:Key="listboxStyle">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=ScrollBarVisibility}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ItemsPresenter Margin="0" />
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ListBox x:Name="listbox1" Style="{StaticResource listboxStyle}" ItemsSource="{TemplateBinding Content}" SelectedIndex="0"/>