如何禁用ListBox中的选择?
我按照这个关于如何将滚动条添加到ItemsControl的小"教程",它在Designer视图中工作,但是在我编译和执行程序时却没有(只显示前几个项目,没有滚动条可以查看更多 - 甚至当VerticalScrollbarVisibility设置为"Visible"而不是"Auto"时).
关于如何解决这个问题的任何想法?
这是我用来显示我的项目的代码(通常我使用Databinding,但是为了查看我的Designer中的项目,我手动添加它们):
<ItemsControl x:Name="itemCtrl" Style="{DynamicResource UsersControlStyle}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Top">
</StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<uc:UcSpeler />
<uc:UcSpeler />
<uc:UcSpeler />
<uc:UcSpeler />
<uc:UcSpeler />
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
这是我的模板:
<Style x:Key="UsersControlStyle" TargetType="{x:Type ItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud) 仍在和WPF一起玩弄,并在我学习的同时学习.现在尝试构建一个动态的控件分组(主要是按钮,但可能包括CheckBoxes和其他).
我不知道最好的方法是什么,所以我尝试创建ItemsControl样式,然后将项目添加到WrapPanel内的ItemsPresenter中.很快意识到这些项目不会换行,因为它们实际上不在WrapPanel中,除非我把它作为ItemsHost.像这样:
<Style x:Key="ButtonPanelGroup" TargetType="{x:Type ItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<Border CornerRadius="5"
BorderBrush="{StaticResource DarkColorBrush}"
BorderThickness="1"
Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<WrapPanel IsItemsHost="True" FlowDirection="LeftToRight">
<ItemsPresenter />
</WrapPanel>
<ContentPresenter ContentSource="Name" Grid.Row="1" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
请注意,这是一项正在进行的工作,我仍然需要实现许多样式效果.我在这里用它:
<UniformGrid Rows="1">
<ItemsControl Name="Group1" Style="{StaticResource ButtonPanelGroup}">
<Button>Button1</Button>
<Button>Button2</Button>
<CheckBox>TickBox</CheckBox>
</ItemsControl>
<ItemsControl Name="Group2" Style="{StaticResource ButtonPanelGroup}">
<Button>Button3</Button>
<Button>Button4</Button>
<Button>Button5</Button>
</ItemsControl>
<ItemsControl Name="Group3" Style="{StaticResource ButtonPanelGroup}">
<Button>Button6</Button>
<Button>Button7</Button>
<Button>Button8</Button>
</ItemsControl>
</UniformGrid>
Run Code Online (Sandbox Code Playgroud)
另请注意,它仍然是一项正在进行中的工作,因为UniformGrid不会成为这里的方式,并且边距可能是一种痛苦(是否有任何重叠的边距?)所以输入会受到赞赏.
现在到了真正的问题.这不起作用我收到错误:
'ItemsPresenter'对象无法添加到'WrapPanel'.无法显式修改用作ItemsControl的ItemsPanel的Panel的Children集合.ItemsControl为Panel生成子元素.对象'System.Windows.Controls.ItemsPresenter'出错.
那么做这样的事情的最佳方法是什么(希望能够将按钮和其他控件扔进ItemControl并且排队真的很棒).将控件放入某种集合并迭代会更好吗?
我希望能很好地完成它,但我的WPF技能仍然缺乏.是否有任何WPF书籍超出了基础知识,并展示专业人士将如何真正做到这一点?
我有一个ListBox可能有很多行的模板化DB记录,包括一个Image,绑定到一个ObservableCollection<MyItem>.有时收藏可以容纳数千件物品.
性能很好,但滚动是默认的跳跃行为.我希望它有平滑的滚动,所以我取消选中ScrollViewer.CanContentScroll.
现在我有平滑的滚动,但性能很可怕:数据在一个单独的线程中检索,并且线程快速完成,但结果显示在中需要10-20秒ListBox.我假设这是因为取消选中ScrollViewer.CanContentScroll将底层更改VirtualizingStackPanel为常规StackPanel,因此在显示结果之前加载整个集合.
所以我的问题是:如何在不牺牲VirtualizingStackPanel行为和性能的情况下保持平滑滚动?
棘手的部分是每个项目都有一个ContextMenu我仍然想要在右键单击时打开的项目(我只是不希望它选择它).
事实上,如果它让事情变得更容易,我根本不需要任何自动选择,所以如果有某种方式我可以完全禁用它,那就没事了.
我只想切换到一个ItemsControl实际的,只要我可以使用虚拟化和滚动来使用它.
我们有一个桌面应用程序,我们有一个ListView,它ListView.ItemTemplate是一个扩展的KPageScrollViewerScrollViewer
虽然我们设定VirtualizingStackPanel.IsVirtualizing="True"和 VirtualizingStackPanel.VirtualizationMode="Recycling"
我们注意到KPageScrollViewer的构造函数(在其中ItemTemplate)总是在查看ListView中的项目时执行.
我们期望它将被创建4到5次,然后,相同的实例将用于查看数据,因为我们使用的是回收模式,但这不会发生,因此我们最终会得到越来越多的KPageScrollViewer实例创造..
是因为我们定制了ListView.ItemsPanel吗?
<ListView.ItemsPanel>
<ItemsPanelTemplate >
<p:KVirtualizingStackPanel IsItemsHost="True"
Run Code Online (Sandbox Code Playgroud)
有什么好主意吗?我想知道哪些原因会导致回收功能丢失?
<ListView x:Class="KETAB.KStudio.Stage.PagesView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:KETAB.KStudio.Stage"
Name="PagesList" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"
Loaded="instScroll_Loaded"
AllowDrop="True"
MouseMove="PagesList_MouseMove"
ScrollViewer.PanningMode="None"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.CleanUpVirtualizedItem="PagesList_CleanUpVirtualizedItem"
VirtualizingStackPanel.VirtualizationMode="Recycling"
>
<ListView.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/StageResources.xaml"/>
<ResourceDictionary Source="/KETAB.KStudio.UserControls;component/ScrollViewerStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Opacity="0.4" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Opacity="0.4" Color="Transparent" />
<!--<p:PageWidthConverter x:Key="PageWidthConverter" />-->
<p:PageWidthConverter x:Key="pageWidthConverter" />
<p:PageHeightConverter x:Key="pageHeightConverter" />
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding …Run Code Online (Sandbox Code Playgroud) 我ListView在窗户上。该ListView的默认ItemsPanel被替换WrapPanel。我也有DataTemplate它的ListViewItem秒。在运行系统中,主窗口将在一段时间内不响应,因为它ListView具有700个(并不断增加)ListViewItem(来自数据绑定)。有没有办法使主窗口保持响应?
可选:ListView未准备好时,我希望在ProgressBar上方显示一条文本(或可能的话),ListView并说“请稍候...”或“正在加载物品...”之类的内容。
XAML:
<ListView x:Name="MyListView" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" HorizontalAlignment="Left" Height="577" VerticalAlignment="Top" Width="902" ScrollViewer.HorizontalScrollBarVisibility="Auto" Foreground="Black" Margin="10,10,0,0" ScrollViewer.CanContentScroll="True" BorderBrush="#FFC54B4B" BorderThickness="3" Background="White">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel MaxWidth="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListView.View).ItemWidth,
RelativeSource={RelativeSource AncestorType=ListView}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight,
RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
Run Code Online (Sandbox Code Playgroud)
编辑:
我尝试了这个:
List<something> MyList = new List<something>();
ThreadPool.QueueUserWorkItem(_ =>
{
( Create MyList here...)
Dispatcher.BeginInvoke(new Action(() => …Run Code Online (Sandbox Code Playgroud) 我有一个ListView用于显示字段列表的wpf; 基于属性值,某些字段可以在运行时折叠.它的工作正常,但ListVIew不会折叠为在运行时折叠的ListViewItem保留的空间.

我能够在Snoop中看到额外的ListViewItems(可见性为折叠),ListView也会向上移动项目,但它不会调整其高度以移除空白区域!
我可以肯定地说,由于VirtualizedStackPanel将ItemsPanel更改为StackPanel解决了这个问题,这种情况正在发生.这是相关的ListViewXAML:
<ListView
x:Class="Wizards.FieldBinderModelListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Margin="0"
VerticalAlignment="Top"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Top"
Background="White"
BorderThickness="0"
Grid.IsSharedSizeScope="True"
KeyboardNavigation.DirectionalNavigation="Continue"
Padding="1"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
SelectionChanged="ListViewSelectionChanged"
SelectionMode="Single">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<!--Works fine with StackPanel but not with VirtualizingStackPanel
Explicitly added this PanelTemplate to show that it works with
StackPanel;ListView uses VirtualizingStackPanel as default panel
and causes same problem-->
<!--<StackPanel Orientation="Vertical" VerticalAlignment="Top"/>-->
<VirtualizingStackPanel Orientation="Vertical"
VerticalAlignment="Top"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Black" />
</Trigger>
<DataTrigger Binding="{Binding …Run Code Online (Sandbox Code Playgroud) 有人可以告诉我如何在WPF中正确使用VirtualizingStackPanel吗?我在ItemsPanelTemplate中为我的ItemsControl设置了一个VirtualizingStackPanel,并将其设置为我的ItemsHost,但是当我在我的item控件上注册一个CleanUpVirtualizedItem附加事件的监听器时,没有任何反应.我没有看到事件被调用.我也没有看到我的数据模板中的自定义控件上的任何Unloaded事件被调用,这表明我没有实际的虚拟化.任何帮助将非常感激.
如何禁用Listview中元素的选择?
当我在ListView中单击elem时,我不想更改背景.你能帮助我吗?
<ListView Name="milestone_listView" Margin="817,108,90,276" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" ItemsSource="{Binding}">
<Grid Name="milestone_grid"></Grid>
</ListView>
Run Code Online (Sandbox Code Playgroud) 我正在使用 ItemsControl 来显示项目列表,itrs xaml 就像
<ItemsControl ItemsSource="{Binding ShelfItemsCollection}" Name="shelfGridView" Margin="5" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Stackpanel>
<Image Width="150" Height="200" Stretch="Fill" Source="{Binding CoverImage}" ></Image>
+
some other infos
</Stackpanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
Run Code Online (Sandbox Code Playgroud)
我面临的问题是我的列表中有近 100 个项目,我正在根据某些属性对列表进行一些过滤操作,并将结果限制为较小的否(例如一次 20 个项目)进行此过滤需要花费一些时间很多时间刷新和加载列表视图。这是我用于过滤的代码
ICollectionView dataView = CollectionViewSource.GetDefaultView(shelfGridView.ItemsSource);
dataView.Filter = CloudFilter;
dataView.Refresh();
private bool CloudFilter(object item)
{
MyObject lib = item as MyObject;
return lib.Property !=valuetofilter;
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以提高性能或渲染速度慢有什么具体原因吗?
我有这样的代码:(将自定义对象的集合加载到内存和列表框项目中)
public class Product : INotifyPropertyChanged
{
// these four doesn't matter, just Product's simple data
public string nDB_No { get; set; }
public string fdGrp_Cd { get; set; }
public string long_Desc { get; set; }
public int refuse { get; set; }
// I do not load this Collection right away, only after explicit call
public ObservableCollection<Ingredient> ingredients { get; set; }
public Product() {sets all null}
public static ObservableCollection<Product> LoadProductsFromList(List<string> productList) {gets products data from SQLServer DB} …Run Code Online (Sandbox Code Playgroud) wpf ×12
.net ×4
listbox ×4
c# ×3
itemscontrol ×3
listview ×3
itemspanel ×2
scroll ×2
wpf-controls ×2
.net-4.0 ×1
c#-4.0 ×1
datatemplate ×1
performance ×1
recycle ×1
scrollviewer ×1
stopwatch ×1
styles ×1