早上好,stackoverflow,
我当前遇到ItemsControl周围的滚动条问题。当ItemsControl内容(绑定到用ListItemViewModel实例填充的BindableCollection)溢出我的窗口边界时,滚动条会显示,但它看起来已禁用(中心没有较小的可拖动条)。因此,我无法滚动浏览扩展内容。展开窗口显示以下内容。我曾尝试将CanContentScroll设置为true和false,但都不起作用。
这是我的基本观点:
<Controls:MetroWindow x:Class="DataIntegrator.Views.BaseView"
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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cal="http://www.caliburnproject.org"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf">
<Grid AllowDrop="True" cal:Message.Attach="[Event Drop] = [Action AddItems($eventArgs)]" Height="Auto" Width="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="14*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="28"/>
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Stretch">
<ItemsControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding List}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl cal:View.Model="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
<Button x:Name="Reprocess" Content="Reprocess Selected Elements" Grid.RowSpan="1" Grid.Row="1" Grid.ColumnSpan="1" Grid.Column="1"/>
</Grid>
正在添加到ItemsControl:的项目的视图
<UserControl x:Class="DataIntegrator.Views.ListItemView"
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:local="clr-namespace:DataIntegrator.Views"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Height="20" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="1"/>
<ColumnDefinition/>
<ColumnDefinition Width="1"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="File" Grid.ColumnSpan="1" Padding="3" Grid.Column="0" Background="#FF5A5A5A" Foreground="Cyan" ></TextBlock>
<TextBlock x:Name="Type" Grid.ColumnSpan="1" Padding="3" Grid.Column="2" Foreground="Cyan" Background="#FF5A5A5A"></TextBlock>
<Rectangle Grid.Column="4" Fill="#FF5A5A5A" ></Rectangle>
<CheckBox x:Name="Reprocess" HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" Width="18" Grid.Column="4" IsChecked="{ Binding Path=Reprocess, Mode=TwoWay }"/>
</Grid>
以及添加列表项的代码:
public void AddToList(string filePath)
{
List.Add(new ListItemViewModel(_eventAggregator){File=filePath});
NotifyOfPropertyChange(() => List);
}
其中变量List已声明并实例化为BindableCollection。
我相信我可能在caliburn.micro上做错了什么,因为以下情况成立:
- 在不更改周围XAML的情况下,只需手动将ItemsControl.Items添加到ItemsControl即可实现预期的滚动条行为
- 设置ItemsPanelTemplate StackPanel的高度将允许预期的滚动条行为,直到我的可绑定集合填充区域并溢出窗口(此时,可用的滚动条将替换为没有可拖动条的滚动条)
在我看来,当项目溢出屏幕时,会出现不同的滚动条,但如果我删除周围的ScrollViewer,那么当屏幕溢出时,就根本没有滚动条了。
有点困惑下一步该去哪里,任何意见都将不胜感激。
谢谢!
将ScrollViwer添加到ControlTemplate
<ItemsControl >
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
所以为了清楚起见,我已经在下面的XAML中测试了它,它对我来说很好,因为我可以查看滚动查看器和滚动。
查看
<Grid Height="200">
<ItemsControl Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding List}">
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="MynewTest"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel ScrollViewer.CanContentScroll="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
ViewModel
public class ViewModel
{
public ViewModel()
{
List = new ObservableCollection<string>() { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
}
private ObservableCollection<string> _MyProperty;
public ObservableCollection<string> List
{
get { return _MyProperty; }
set { _MyProperty = value; }
}
}