我在ListView中有许多UserControls。即使没有ScrollViewer,WPF仍以某种方式实现了水平滚动。当我尝试实现垂直滚动时,ScrollViewer呈灰色且无法使用。我尝试将ListView包装在ScrollViewer中,但它不会滚动。 即使删除ListView并仅尝试在其中放置带有Textboxes的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>
答案 0 :(得分:0)
这对我来说很好。也许您没有显示其余的代码。这是我从您的示例中得出的结果,并且可以按预期工作...
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>
ViewModel:
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; }
}
}
如果您调整窗口的大小,则当宿主控件无法再显示所有控件时,滚动条就会出现。几乎是标准的东西。
答案 1 :(得分:0)
ListView / Listbox / Stackpanel:可以根据其内容的大小进行扩展。
将列表视图/列表框包装在高度为“ *”的网格中(高度属性不要使用“自动”,它会根据其内容使其像堆栈面板一样展开)