WPF - Scrollviewer 内的列表框不会滚动项目
WPF - Listbox inside Scrollviewer wont scroll Items
我在 ListView 中有许多 UserControl。即使没有 ScrollViewer,WPF 也以某种方式实现了水平滚动。当我尝试实现垂直滚动时,ScrollViewer 是灰色的并且无法使用。我尝试将 ListView 包装在 ScrollViewer 中,但它不会滚动。
即使我删除了 ListView 并且只尝试在其中放置一个带有文本框的 StackPanel,ScrollViewer 仍然被禁用。
有什么我想念的吗?
XAML:
<ScrollViewer VerticalScrollBarVisibility="Visible" CanContentScroll="True"
Grid.Row="1" Grid.Column="1">
<ListBox Margin="0,0,10,10" ItemsSource="{Binding Feeder}"
Grid.RowSpan="3">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:FeederControl FeederName="{Binding FeederName}"
AxisList="{Binding AxisList}"></local:FeederControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
这对我来说很好用。也许您没有显示其余代码。这是我根据您的示例所做的,它按预期工作...
XAML:
<Window x:Class="WPF_Playground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
</Window>
视图模型:
using System.Collections.Generic;
using System.Windows;
namespace WPF_Playground
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public IEnumerable<Item> Items => new Item[]
{
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" }
};
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
public class Item
{
public string Text { get; set; }
}
}
如果您调整 window 的大小,当托管控件无法再显示所有控件时,将出现滚动条。几乎是标准的东西。
ListView/Listbox/Stackpanel : 可以根据其内容的大小进行扩展。
将您的 Listview/listbox 包裹在高度为“*”的网格中(不要使用 "Auto" 作为高度 属性,它会像堆叠面板一样展开,基于其内容)
我在 ListView 中有许多 UserControl。即使没有 ScrollViewer,WPF 也以某种方式实现了水平滚动。当我尝试实现垂直滚动时,ScrollViewer 是灰色的并且无法使用。我尝试将 ListView 包装在 ScrollViewer 中,但它不会滚动。 即使我删除了 ListView 并且只尝试在其中放置一个带有文本框的 StackPanel,ScrollViewer 仍然被禁用。 有什么我想念的吗?
XAML:
<ScrollViewer VerticalScrollBarVisibility="Visible" CanContentScroll="True"
Grid.Row="1" Grid.Column="1">
<ListBox Margin="0,0,10,10" ItemsSource="{Binding Feeder}"
Grid.RowSpan="3">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:FeederControl FeederName="{Binding FeederName}"
AxisList="{Binding AxisList}"></local:FeederControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
这对我来说很好用。也许您没有显示其余代码。这是我根据您的示例所做的,它按预期工作...
XAML:
<Window x:Class="WPF_Playground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
</Window>
视图模型:
using System.Collections.Generic;
using System.Windows;
namespace WPF_Playground
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public IEnumerable<Item> Items => new Item[]
{
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" },
new Item{ Text = "Something" }
};
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
public class Item
{
public string Text { get; set; }
}
}
如果您调整 window 的大小,当托管控件无法再显示所有控件时,将出现滚动条。几乎是标准的东西。
ListView/Listbox/Stackpanel : 可以根据其内容的大小进行扩展。
将您的 Listview/listbox 包裹在高度为“*”的网格中(不要使用 "Auto" 作为高度 属性,它会像堆叠面板一样展开,基于其内容)