Wpf 无背景滚动查看器
我想问你是否有人知道如何创建无背景的ScrollViewer。我知道使用样式或模板(我猜)是不可能的,必须从ScrollVewer创建新组件,但我找不到哪个方法/属性重写 例如:Wpf 无背景滚动查看器,wpf,background,scrollviewer,Wpf,Background,Scrollviewer,我想问你是否有人知道如何创建无背景的ScrollViewer。我知道使用样式或模板(我猜)是不可能的,必须从ScrollVewer创建新组件,但我找不到哪个方法/属性重写 例如: <Window x:Class="WpfScroll.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/
<Window x:Class="WpfScroll.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<Button x:Name="bt"></Button>
<ScrollViewer Background="{x:Null}" Margin="20">
<StackPanel Orientation="Horizontal" Background="{x:Null}">
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
</StackPanel>
</ScrollViewer>
</Grid>
</Window>
我想要的是能够点击按钮“bt”之间的矩形背景
谢谢你的建议。
-pav-您可以处理ScrollViewer的PreviewMouseDown事件。 在您的xaml中:
<Grid>
<!--Button is removed as it will be not used-->
<ScrollViewer Background="{x:Null}" Margin="20" PreviewMouseDown="ScrollViewer_MouseDown">
<StackPanel Orientation="Horizontal" Background="{x:Null}">
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
</StackPanel>
</ScrollViewer>
</Grid>
我希望它能帮助您。您可以处理ScrollViewer的PreviewMouseDown事件。 在您的xaml中:
<Grid>
<!--Button is removed as it will be not used-->
<ScrollViewer Background="{x:Null}" Margin="20" PreviewMouseDown="ScrollViewer_MouseDown">
<StackPanel Orientation="Horizontal" Background="{x:Null}">
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
</StackPanel>
</ScrollViewer>
</Grid>
我希望它能对您有所帮助。按原样复制粘贴以下样式:
<Window.Resources>
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Stylus.IsFlicksEnabled"
Value="false" />
<Setter Property="Foreground"
Value="#ADABAB" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="Width"
Value="7" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="GridRoot"
Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="0.00001*" />
</Grid.RowDefinitions>
<Border x:Name="CornerScrollBarRectangle"
CornerRadius="5" BorderThickness="1" BorderBrush="Brown"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="Auto"
Height="Auto"
Margin="0,1,0,1"
Background="Transparent" />
<Track x:Name="PART_Track"
Grid.Row="0"
IsDirectionReversed="true"
Focusable="false">
<Track.Thumb>
<Thumb x:Name="Thumb"
Background="{TemplateBinding Foreground}"
Style="{DynamicResource ScrollBarTrackThumb}" />
</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>
<Style x:Key="ScrollBarTrackThumb"
TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Grid x:Name="Grid" Background="Transparent">
<Rectangle HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="Auto"
Height="Auto"
Fill="Transparent" />
<Border x:Name="CornerScrollBarRectangle"
CornerRadius="5" BorderThickness="1" BorderBrush="Yellow"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="Auto"
Height="Auto"
Margin="0,1,0,1"
Background="Transparent" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Tag"
Value="Horizontal">
<Setter TargetName="CornerScrollBarRectangle"
Property="Width"
Value="Auto" />
<Setter TargetName="CornerScrollBarRectangle"
Property="Height"
Value="6" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
按原样复制粘贴以下样式:
<Window.Resources>
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Stylus.IsFlicksEnabled"
Value="false" />
<Setter Property="Foreground"
Value="#ADABAB" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="Width"
Value="7" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="GridRoot"
Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="0.00001*" />
</Grid.RowDefinitions>
<Border x:Name="CornerScrollBarRectangle"
CornerRadius="5" BorderThickness="1" BorderBrush="Brown"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="Auto"
Height="Auto"
Margin="0,1,0,1"
Background="Transparent" />
<Track x:Name="PART_Track"
Grid.Row="0"
IsDirectionReversed="true"
Focusable="false">
<Track.Thumb>
<Thumb x:Name="Thumb"
Background="{TemplateBinding Foreground}"
Style="{DynamicResource ScrollBarTrackThumb}" />
</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>
<Style x:Key="ScrollBarTrackThumb"
TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Grid x:Name="Grid" Background="Transparent">
<Rectangle HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="Auto"
Height="Auto"
Fill="Transparent" />
<Border x:Name="CornerScrollBarRectangle"
CornerRadius="5" BorderThickness="1" BorderBrush="Yellow"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="Auto"
Height="Auto"
Margin="0,1,0,1"
Background="Transparent" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Tag"
Value="Horizontal">
<Setter TargetName="CornerScrollBarRectangle"
Property="Width"
Value="Auto" />
<Setter TargetName="CornerScrollBarRectangle"
Property="Height"
Value="6" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
好的,您可以使用以下示例:
<Window x:Class="scrollviewerbackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:scrollviewerbackground"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="bt"></Button>
<local:ScrollViewerClickable HorizontalScrollBarVisibility="Visible" Margin="20">
<StackPanel IsHitTestVisible="True" Orientation="Horizontal" Background="{x:Null}">
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
</StackPanel>
</local:ScrollViewerClickable>
</Grid>
好的,您可以使用以下示例:
<Window x:Class="scrollviewerbackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:scrollviewerbackground"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="bt"></Button>
<local:ScrollViewerClickable HorizontalScrollBarVisibility="Visible" Margin="20">
<StackPanel IsHitTestVisible="True" Orientation="Horizontal" Background="{x:Null}">
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
<Rectangle Fill="Red" Width="100" Margin="50"></Rectangle>
</StackPanel>
</local:ScrollViewerClickable>
</Grid>
好吧,我想您知道可以将ishitsetvisible=“False”设置为scrollviewer。问题是,scrollviewer会吞噬您的鼠标事件。是的,我知道,但正如您所说,鼠标事件将消失,我需要它们。嗯,我想您知道可以将IshittetVisible=“False”设置为scrollviewer。问题是,scrollviewer会吞噬你的鼠标事件。是的,我知道,但正如你所说,鼠标事件