我将以下语句放在xaml的网格的第二行中:
<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1">
<ListView Name="listView" Margin="5" Grid.Row="1">
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridViewColumn DisplayMemberBinding="{Binding Path=DateTime}" Header="Date Time" Width="140"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Vehicle}" Header="Vehicle" Width="130"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=AlarmType}" Header="Alarm Type" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Direction}" Header="Direction" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Speed}" Header="Speed" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Alarmed}" Header="Alarmed" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=LoadType}" Header="Load Type" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Status}" Header="Status" Width="110"/>
</GridView>
</ListView.View>
</ListView>
</ScrollViewer>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我将listView.ItemSource绑定到代码中定义的ObservableCollection,以将数据填充到列表中.当添加到GridView的项目数超过列表视图高度时,垂直滚动条没有像我在XAML中指定的那样出现.我做错了什么?非常感谢您的意见.谢谢.
我有一个垂直的 WPF 列表框,绑定到一个集合,带有一个自定义的 ItemTemplate。我注意到了一些事情:
与独立的滚动查看器不同,列表框不会连续滚动,而是以等于项目高度的增量跳跃。这种差异在 XAML 中的何处定义(列表框模板毕竟只包含一个滚动查看器?
是否可以为这个跳跃设置动画?我正在尝试对此进行动画处理,以便它可以快速为从一个偏移量跳转到另一个偏移量设置动画。锦上添花的是动画在最后显示一些内部和“摆动”。我希望会有一个预定义的行为,但似乎没有。尝试自己在带有 BeginAnimation 的代码中执行此操作失败,抛出的异常告诉我无法对 Scrollviewer 的 VerticalOffset 属性进行动画处理。
有什么线索吗?
谢谢汤姆
我有一个问题,当TreeView放置在具有ScrollViewer的容器内时,TreeView的滚动条不起作用,而是调整容器的ScrollViewer的内容,以便所有TreeView项都可见.
在WinForms中我可以设置容器的最小内容宽度和高度,但是如何在WPF中实现这一点?
这是XAML的示例:
<Window x:Class="TestWpfTreeViewInsideScrollViewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ScrollViewer Name="scroll1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
<Grid MinHeight="230" MinWidth="200" Grid.IsSharedSizeScope="True">
<Button Content="Button" Width="74" Height="52" Margin="10,24,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Content="Button" Height="52" Margin="89,24,10,0" VerticalAlignment="Top"/>
<Button Content="Button" Width="74" Height="52" Margin="10,81,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TreeView Margin="10,138,10,55">
<TreeViewItem IsExpanded="True" Header="This is a long text but the treeview is not scrolling">
Instead the TreeView is expanding its size to fit all these nodes inside it, i want that the upper scroller take in only when grid minwidth …Run Code Online (Sandbox Code Playgroud) 我在缩放图像后更新了ScrollViewer中的一个大问题.我试过调用方法UpdateLayout,但一切都完好无损.图像没有问题缩放,但滚动条保持不变.
几个小时,我在堆栈溢出和谷歌上寻找不同的解决方案,但没有我失败.XAML:
<ScrollViewer x:Name="imagescrl" Grid.Row="1" VerticalScrollBarVisibility="Auto" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" >
<Image x:Name="imgp" VerticalAlignment="Center" HorizontalAlignment="Center">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0.01" ScaleY="0.01"/>
<ScaleTransform x:Name="imgpScale">
<ScaleTransform.ScaleX>
<Binding ElementName="sldZoom" Path="Value" Mode="OneWay"/>
</ScaleTransform.ScaleX>
<ScaleTransform.ScaleY>
<Binding ElementName="sldZoom" Path="Value" Mode="OneWay"/>
</ScaleTransform.ScaleY>
</ScaleTransform>
</TransformGroup>
</Image.RenderTransform>
</Image>
</ScrollViewer>
...
...
<Slider ValueChanged="Slider_ValueChanged" x:Name="sldZoom" BorderThickness="11,20,0,5" Maximum="250" Minimum="1" Value="100" Width="153"/>
Run Code Online (Sandbox Code Playgroud)
谢谢
C#:
private void Slider_ValueChanged(object sender,RoutedPropertyChangedEventArgs<double> e)
{
imagescrl.UpdateLayout();
}
Run Code Online (Sandbox Code Playgroud) 我有一个SurfaceListBox里面有一个ScrollViewer。我可以访问它,ScrollViewer并且我想检测滚动结束的时间。由于它是一个触摸应用程序,可以拖动它,因此,我现在正在检测到PreviewTouchUp要执行的操作,但是,如果像我在智能手机上那样快速滑动,因为的内容ScrollViewer仍在拖动/ scrolling,PreviewTouchUp不会反映滚动结束,而是在之前触发。
我曾尝试Manipulation事件和其他几个人同时在ScrollViewer和SurfaceListBox。我也尝试过ScrollViewer.PanningDeceleration,说实话,我看不出有什么不同。到目前为止,基本上没有运气。
因此,即使在PreviewTouchUp中,我也如何检测滚动的结束ScrollViewer?
这就是我想要做的。我有一个 Scrollviewer,里面嵌套了 UserControls,嵌套在其他 UserControls、网格、堆栈面板和其他容器中。当我单击在此层次结构中的某处添加另一个子项的按钮时,我想滚动以查看该子项。函数代码如下:
public static void ScrollParentNamedScrollViewerDown(DependencyObject child, string strTargetParent, DependencyObject newStartPoint = null)
{
if(child == null) return;
if(newStartPoint == null) newStartPoint = child;
ScrollViewer scvPotentialTarget = GetParentOfType<ScrollViewer>(newStartPoint);
if (scvPotentialTarget == null) return;
if (scvPotentialTarget.Name != strTargetParent)
{
ScrollParentNamedScrollViewerDown(child, strTargetParent, scvPotentialTarget);
}
else
{
UIElement scrollTarget = child as UIElement;
if (scrollTarget == null)
scvPotentialTarget.ScrollToBottom();
else{
Point pTarget = scrollTarget.TranslatePoint(new Point(0, 0), scvPotentialTarget);
if (pTarget == null)
scvPotentialTarget.ScrollToBottom();
else
scvPotentialTarget.ScrollToVerticalOffset(pTarget.Y);
}
}
Run Code Online (Sandbox Code Playgroud)
这个函数是这样使用的:
gbSubWindow.Visibility = System.Windows.Visibility.Visible;
gbSubWindow.Content = …Run Code Online (Sandbox Code Playgroud) 我在包含 3GroupBoxe秒的网格中有一个列。最后GroupBox一个Grid包含两个元素:aButton和 a TreeView(垂直)。该TreeView/折叠应该有一个动态的高度,因为它包含可展开的元件。
包含所有元素的窗口可以由用户调整大小。
如果窗口太小,你就看不到所有的分组框,所以我需要一个滚动条。我可以向下滚动,但是如果我想TreeView通过鼠标滚轮在里面滚动,则什么也不会发生。
这是我的代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<GroupBox Header="Test 1" Grid.Row="0">...</GroupBox>
<GroupBox Header="Test 2" Grid.Row="1">...</GroupBox>
<GroupBox Header="Test 3" Grid.Row="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<CheckBox Content="All" Grid.Row="0"/>
<TreeView x:Name="NameTree"
Grid.Row="1"
ItemsSource="{Binding Names}"/>
</Grid>
</GroupBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
解决方法可能是将第 3 行设置为固定大小,而不是使用 *. 在这种情况下,我将使用两个滚动条(ScrollViewer 和 TreeViews 滚动条,但我想要第三个 groupbox 的动态高度。
我有StackPanel和X ListViews.
每个ListView必须滚动垂直.
StackPanel必须水平滚动.
<StackPanel Column="1" Grid.Row="1" Orientation="Horizontal">
<ListView MinWidth="200" Margin="5,5,5,5">
</ListView>
<ListView MinWidth="200" Margin="5,5,5,5">
</ListView>
...
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
使用此代码ListViews滚动工作正常.
如果我将StackPanel放在ScrollViewer中,则ListViews滚动不起作用.
我有这个屏幕,用户可以更改高度,基于此,我希望ScrollViewer在需要时显示滚动条,但它的大小始终相同。
请注意,只有父网格的第二行会更改大小(*),并且基于该大小我想要我的大小,并且基于滚动条应显示ScrollViewer的内部网格内容(通过代码动态添加)。ScrollViewer
<Grid Style="{StaticResource PopupBody}">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<!-- Header Panel -->
<StackPanel x:Name="PopupHeader"
Grid.Row="0">
<Label x:Name="PopupTitle"
Style="{StaticResource PopupTitle}"
Content="Column Updatable Detail"/>
</StackPanel>
<!-- Body Panel -->
<DockPanel x:Name="PopupBody"
Grid.Row="1"
Margin="10,10,10,0" Height="350" VerticalAlignment="Top">
<StackPanel DockPanel.Dock="Top" Margin="{StaticResource MarginSmallHorizontal}">
<Grid Margin="{StaticResource MarginSmallVertical}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="250" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
(... Hidden to be more readable ... ) …Run Code Online (Sandbox Code Playgroud) 我有以下相当简单的代码
<Window ... Width=400 Height=400>
<ScrollViewer HorizontalScrollBarVisibility="Auto" >
<StackPanel VerticalAlignment="Top"
HorizontalAlignment="Left">
<TextBox TextWrapping="Wrap"
Margin="0,5,0,5"
Width="500"
Padding="20">Scrolling is enabled when it is necessary.
Resize the window, making it larger and smaller.</TextBox>
<StackPanel Orientation="Horizontal">
<Label Content="aswkognweklng"></Label>
<TextBox TextWrapping="Wrap"
Margin="0,5,0,5"
Width="500"
Padding="20">Scrolling is enabled when it is necessary.
Resize the window, making it larger and smaller.</TextBox>
</StackPanel>
</StackPanel>
</ScrollViewer>
</Window>
Run Code Online (Sandbox Code Playgroud)
我想禁用以下行为:
=> 滚动查看器将移动滚动条,使文本框左边框与可见窗口边框对齐
我想禁用这种自动滚动行为。那可能吗?
用户交互的默认滚动行为应该仍然有效。因此,当用户与滚动条交互时,它应该正常滚动内容。