我有一个列表框,里面有很多渲染成本很高的项目。然而,VirtualizingStackPanel 通过只渲染可见项来处理这个问题。我想覆盖 ScrollViewer 的控件模板,因为默认的控件模板在水平和垂直滚动条之间有灰色矩形。我只是复制了微软提供的一个(ScrollViewer ControlTemplate Example),它没有灰色矩形问题。
然而,这个控制模板通过给 VirtualizingStackPanel 无限的高度来禁用虚拟化。这意味着 VirtualizingStackPanel 将呈现所有项目,因为它认为所有项目都是可见的。
在下面的演示代码中,我在列表框中显示了 10000 个项目。我通过比较运行它与 ScrollViewer 样式和没有它来验证问题。有了它,演示运行得非常慢,调整大小需要几秒钟。没有样式它非常快。我输出了一些关于 VirtualizingStackPanel 的信息来证明我的观点:
没有 ScrollViewer 样式(注释掉 XAML 中的样式):
ViewportHeight: 8
ExtentHeight: 10000
ActualHeight: 245
IsVirtualizing: True
VirtualizationMode: Standard
使用 ScrollViewer 样式:
ViewportHeight: 0
ExtentHeight: 0
ActualHeight: 272766.666666707
IsVirtualizing: True
VirtualizationMode: Standard
知道如何为不会与虚拟化混淆的 ScrollViewer 编写控件模板吗?
XAML:
<Window x:Class="VirtualTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="{x:Type ScrollViewer}" TargetType="{x:Type ScrollViewer}">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition …Run Code Online (Sandbox Code Playgroud) 我今天要面对的是滚动查看器,事实上它会阻止我的事件。SO..这里有一些 xaml:
<ScrollViewer x:Name="scrollv" Panel.ZIndex="15" Margin="8,65.5,0,22" VerticalScrollBarVisibility="Visible" Height="392.5" Background="White" IsHitTestVisible="False">
<Grid x:Name="listaintrebari" Height="Auto" Width="301.5" HorizontalAlignment="Left" VerticalAlignment="Top" Background="White" >
</Grid>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)
所以事情是:在滚动查看器内部的网格上,我以编程方式添加问题 UserControl...其中顺便有一个带有单击事件的按钮。我的问题是我无法单击该按钮...就像滚动查看器一样就像一个隐形的盾牌一样,保护用户控件和按钮免受邪恶鼠标的侵害!
任何帮助表示赞赏!
编辑:(这是我的问题用户控制)
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:eHelper_v3"
mc:Ignorable="d"
x:Class="eHelper_v3.questions"
x:Name="IntrebareControl" Width="330.641" Margin="0" MouseLeftButtonDown="IntrebareControl_MouseLeftButtonDown"
>
<Grid x:Name="LayoutRoot" ScrollViewer.VerticalScrollBarVisibility="Disabled" Margin="0,0,13,0" Height="90" >
<Rectangle x:Name="rect" Panel.ZIndex="20" Height="90" Stroke="#FF0975A3" >
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#1404BFD8" Offset="0.004"/>
<GradientStop Color="#1300A7FF" Offset="0.996"/>
<GradientStop Color="#2B0F82CC" Offset="0.459"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Image x:Name="imagine" HorizontalAlignment="Left" Margin="8,8,0,8" Width="126.667" Stretch="Fill" MouseLeftButtonDown="imagine_MouseLeftButtonDown" />
<TextBlock x:Name="textul" Margin="138.667,8,8,22.5" TextWrapping="Wrap" Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" FontSize="9.333"/>
<Label …Run Code Online (Sandbox Code Playgroud) 我在 wpf 中绘制了一些路径到屏幕上。使用的坐标非常小,因此它们已被制作为用视图框填充屏幕。我现在正在尝试实现平移和缩放功能。我希望能够缩放到鼠标相对于 ui 的任何位置(即缩放的屏幕中心等于鼠标坐标)。当前的结果是缩放时屏幕的中心不能反映 ui 上的确切鼠标位置。
如果你想看看发生了什么......这是我当前的解决方案文件。
或者
继承人一些代码:
查看 Xaml
<Grid Name="MasterGrid" DataContext="{StaticResource mainWindowViewModel}">
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Name="VisualisationScroller">
<Viewbox Name="VisualisationBox" Stretch="Fill" Loaded="VisualisationBox_Loaded">
<ItemsControl Name="CustomDrawingElement" ItemsSource="{Binding Trajectories}" Width="{Binding VisualisationWidth}" Height="{Binding VisualisationHeight}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="data:VisualisedTrajectory">
<Path Data = "{Binding PathData}" Stroke="Red" StrokeThickness="0.001" Fill="Transparent" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas
Background="DarkGray"
IsItemsHost="True">
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1" />
<TranslateTransform />
</TransformGroup>
</ItemsControl.RenderTransform>
</ItemsControl>
</Viewbox>
</ScrollViewer>
</Grid>
Run Code Online (Sandbox Code Playgroud)
查看模型
public class MainWindowViewModel : BaseViewModel
{
public MainWindowViewModel()
{ …Run Code Online (Sandbox Code Playgroud) 我对相应地缩放图像有一点问题。我的ImageXAML 中有一个,ScrollViewer并附有一个,因此可以对其进行缩放。像这样 :
<ScrollViewer x:Name="ImageScrollViewer">
<Image x:Name="ImagePanel"
Stretch="Uniform"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)
我想要做的是,如果图像太大而无法容纳图像控件并且需要缩小(仅在这种情况下),我想以编程方式设置 ScrollViewer 的 ZoomFactor 使其适合图像控件。我的问题是,考虑到我的图像的宽度和高度,我真的不知道如何确定该因素。
另外我不想缩放图片,以任何方式改变它,我在这个问题上看到了一些类似的主题,但到目前为止,没有一个适用于我的案例。我只需要缩小我的图片,直到没有可见的垂直/水平滚动条。
关于如何做到这一点的任何建议,非常感谢,谢谢!
我有一个ScrollViewer,它只垂直滚动,并显示垂直滚动条,如果它只需要:
<ScrollViewer x:Name="sv" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto">
Run Code Online (Sandbox Code Playgroud)
我还有一个Label,我只想显示ScrollViewer的垂直滚动条是否显示:
<Label Background="DarkBlue" Height="60" Width="70">
<Label.Style>
<Style TargetType="Label">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ComputedVerticalScrollBarVisibility.Visibility, ElementName=sv}" Value="Hidden">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用.我已经找到了一个解决方案,通常我很快就找到了,因此这是我的第一篇文章.有关如何使这项工作的任何建议?我更喜欢只有xaml的解决方案,但可以说服使用转换器和什么不是.
我有一个管理锦标赛的应用程序。我有滚动问题的视图如下所示:Group Phase View Screenshot
现在我遇到了当鼠标悬停在 DataGrid 上时无法滚动的问题。例如,只要我尝试在扩展器上滚动 - 它就可以工作(但是一旦鼠标再次越过 DataGrid,滚动就会停止)。ScrollViewer 本身也按预期工作,例如当我尝试使用滚动条本身滚动时。
视图构建如下...
我有一个看起来像这样的 GroupPhaseView(这个视图包含 ScrollViewer):
<UserControl x:Class="TournamentApplication.UI.Tournament.GroupPhase.GroupPhaseView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:groupPhase="clr-namespace:TournamentApplication.UI.Tournament.GroupPhase"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding GroupPhaseCategoryViewModels}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="groupPhase:GroupPhaseCategoryViewModel">
<groupPhase:GroupPhaseCategoryView />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
Run Code Online (Sandbox Code Playgroud)
此 GroupPhaseView 包含多个 GroupPhaseCategoryView。一个 GroupPhaseCategoryView 看起来像这样:
<UserControl x:Class="TournamentApplication.UI.Tournament.GroupPhase.GroupPhaseCategoryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:groupPhase="clr-namespace:TournamentApplication.UI.Tournament.GroupPhase"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Expander Header="{Binding Category.Name}" IsExpanded="True">
<ItemsControl ItemsSource="{Binding GroupPhaseGroupViewModels}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="groupPhase:GroupPhaseGroupViewModel">
<groupPhase:GroupPhaseGroupView />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
</Grid>
Run Code Online (Sandbox Code Playgroud)
此 GroupPhaseCategoryView 包含多个 GroupPhaseGroupView。它们包含两个导致滚动问题的 …
我想在滚动时按住键盘上的 SHIFT 来ScrollViewer水平滚动。
我从这里了解到,该PointerWheelChanged活动就是我正在寻找的活动。然而,处理它是行不通的,因为它是ScrollViewer在内部处理的,所以我的处理程序永远不会被调用。为了解决这个问题,我使用了“路由事件概述”AddHandler一文中所述的方法。
这有效......但似乎在运行ScrollViewer其内部代码后运行我的代码。其结果是ScrollViewer内容先垂直平移,然后水平平移。它们似乎按照这个顺序发生,并且设置e.Handled = true并不能阻止它。
有没有办法“拦截”滚动,以便我可以用自己的逻辑处理它,从而允许在ScrollViewer按下 SHIFT 时水平平移?我最近在这里问了一个类似的问题(涉及拦截控件的输入,以便我可以用自己的逻辑处理它),其中答案涉及处理不同的事件,该事件发生在控件运行其自己的逻辑之前。我没有看到指针滚动的类似“发生前的事情”事件。
我的代码如下。请注意,ScrollViewer可以水平和垂直滚动以及缩放:
<!-- Contained in Grid in a UserControl, if that's relevant -->
<ScrollViewer Name="MyCanvasScrollViewer"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
ZoomMode="Enabled"
ZoomSnapPointsType="Optional"
PointerWheelChanged="MyCanvasScrollViewer_PointerWheelChanged">
<!-- Content to pan -->
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)
隐藏代码:
// Constructor for the user contol.
public MyControl()
{
// Add the scroll wheel event handler and force it to run. …Run Code Online (Sandbox Code Playgroud) 你知道如何使WPF DataGrid VerticalScrollBar始终可见,即使没有数据显示而没有把DataGrid对象instid ScrollViewer?
我想为我的scrollviewer添加边框.仅当ScrollViewer的ScrollBar可见时(VerticalScrollBarVisibility设置为"Auto")才会显示此顺序
谢谢你!
我有一个绑定到滚动查看器的文本块来滚动文本.我希望文本在触发按钮点击等事件时自动滚动而不显示任何用户输入.我尝试使用ScrollToVerticalOffset,但过渡并不顺利.无论如何,我可以使文本顺利向上滚动.
scrollviewer ×10
wpf ×7
c# ×4
binding ×2
datagrid ×2
zooming ×2
image ×1
mouse ×1
routedevents ×1
uwp ×1
viewbox ×1
wpf-controls ×1
xaml ×1