在WPF-ListView控件上,所选项必须可视化一些附加细节。如果选择了项,则需要比ListView控件提供更多的空间。
默认情况下,不可能在选定项内滚动。向下滚动,它直接跳到下一项,并且不可能看到所选项的底部部分。
有人知道如何在所选项目中启用滚动吗?
下面的代码演示了这种行为。在实际代码中,所选项更复杂,但举个例子,所选项的大小在被选中时只会被修改:
XAML:
<Window x:Class="ListViewWithLargeSelectedItem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300">
<Grid>
<ListView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" HorizontalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Border x:Name="border" Padding="10" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Text}" />
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected}"
Value="true">
<Setter TargetName="border" Property="Padding"
Value="40,200" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
代码背后:
public partial class MainWindow : Window
{
private CustomItem _selectedItem;
public CustomItem SelectedItem
{
get { return _selectedItem; }
set
{
if (_selectedItem != null)
{
_selectedItem.IsSelected = false;
}
_selectedItem = value;
_selectedItem.IsSelected = true;
}
}
public List<CustomItem> Items
{
get { return (List<CustomItem>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(List<CustomItem>), typeof(MainWindow), new UIPropertyMetadata(null));
public MainWindow()
{
InitializeComponent();
Items = new List<CustomItem>();
for (int i = 0; i < 10; i++)
{
Items.Add(new CustomItem() { IsSelected = false, Text = "ITEM " + i });
}
DataContext = this;
}
}
public class CustomItem : INotifyPropertyChanged
{
public string Text { get; set; }
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (_isSelected == value)
{
return;
}
_isSelected = value;
NotifyOfPropertyChange("IsSelected");
}
}
private void NotifyOfPropertyChange(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}发布于 2014-01-27 13:42:42
您的问题不是很清楚,但是如果您说您的ListView滚动使用整个项,并且希望它使用像素滚动,那么请在MSDN上查看 property页面。如果是这样的话,那么您只需要在您的ListView上将这个附加属性设置为ListView,以便能够顺利滚动:
<ListView ScrollViewer.CanContentScroll="False" ... />https://stackoverflow.com/questions/21382210
复制相似问题