我有一个ListBox控件,我在网格布局中呈现固定数量的ListBoxItem对象.所以我将ItemsPanelTemplate设置为Grid.
我从后面的代码访问Grid以配置RowDefinitions和ColumnDefinitions.
到目前为止,它都像我期望的那样工作.我有一些自定义的IValueConverter实现,用于返回每个ListBoxItem应该出现的Grid.Row和Grid.Column.
但是我有时会遇到奇怪的绑定错误,我无法弄清楚它们为什么会发生,或者即使它们在我的代码中.
这是我得到的错误:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
谁能解释一下发生了什么?
哦,这是我的XAML:
<UserControl.Resources>
<!-- Value Converters -->
<v:GridRowConverter x:Key="GridRowConverter" />
<v:GridColumnConverter x:Key="GridColumnConverter" />
<v:DevicePositionConverter x:Key="DevicePositionConverter" />
<v:DeviceBackgroundConverter x:Key="DeviceBackgroundConverter" />
<Style x:Key="DeviceContainerStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Grid.Row" Value="{Binding Path=DeviceId, Converter={StaticResource GridRowConverter}}" />
<Setter Property="Grid.Column" Value="{Binding Path=DeviceId, Converter={StaticResource GridColumnConverter}}" />
<Setter Property="Template"> …Run Code Online (Sandbox Code Playgroud) 在WPF中Listbox,我很困惑与这些概念2:
ItemTemplate和ItemsPanelTemplate
一个人能更给我解释一下?
谢谢约翰
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<c:SearchTextBox Grid.ColumnSpan="2" .../>
<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1">
<ListBox
ItemsSource="{Binding Categories}"
IsSynchronizedWithCurrentItem="True"
... />
</ScrollViewer>
<!-- Here is what I'm talking about:-->
<ListBox ItemsSource="{Binding Products}"
IsSynchronizedWithCurrentItem="True" Grid.Column="1" Grid.Row="1">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我想要的是右栏中的项目应该被布置为填充窗口宽度,然后创建一个新的行,这正是WrapPanel的制作.
问题是,WrapPanel只在一行中显示项目,在下面显示水平滚动条,而所有项目在右侧"隐藏"超过窗口大小.
我怎么能防止这种情况?
我需要在控件上设置ItemsPanelTemplate一个ListBox基于a 的属性DependencyProperty.我如何使用它DataTemplateSelector来做到这一点?
我有类似的东西:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<!-- Here I need to replace with either a StackPanel or a wrap panel-->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
Run Code Online (Sandbox Code Playgroud)
这里(Wpf中的ItemsPanelTemplate Selector?)与Similar Question相关联.以下是我的代码但不起作用:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="myXmlDataBase" XPath="/myXmlData">
<x:XData>
<myXmlData xmlns="">
<Item Name = "CoverSheet" SNo="1" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000003.tif"/>
<Item Name = "HCFA" SNo="2" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/>
<Item Name = "HCFA" SNo="3" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/>
<Item Name = "HCFA" SNo="4" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/>
<Item Name …Run Code Online (Sandbox Code Playgroud) 我在ItemsControl ItemsPanelTemplate中定义了一个Grid,其中一个RowDefinitions有ax:Name定义(所以我可以为行大小设置动画).
<ItemsControl ItemsSource="{Binding Data, Source={StaticResource model}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition x:Name="t" />
<RowDefinition />
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
这在3.5中运行良好,但是当我们最近升级到4.0时,它们全都崩溃了.我会看到一个Grid,它定义了Row和Column定义但没有子节点.
如果我在网格上设置IsItemsHost = true,一切都会开始工作.如果我将一个x:Name添加到Grid本身,或者从RowDefinition中删除x:Name,它都可以正常工作.
<ItemsPanelTemplate>
<Grid IsItemsHost="True">
<Grid.RowDefinitions>
<RowDefinition x:Name="t" />
<RowDefinition />
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
Run Code Online (Sandbox Code Playgroud)
要么
<ItemsPanelTemplate>
<Grid x:Name="g">
<Grid.RowDefinitions>
<RowDefinition x:Name="t" />
<RowDefinition />
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
Run Code Online (Sandbox Code Playgroud)
这似乎是一个错误,但我想与社区核实,看看人们是否同意,或者我是否忽视了某些事情.我在Connect或网络上找不到任何内容,所以任何人都可以解释我所看到的内容吗?
我已经UserControl定义了这个XAML并且想ItemsPanelTemplate在我的代码后面动态设置(XAML在示例中不是这样):
<UserControl>
<ItemsControl x:Name="Items">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid> <!-- I want to add this Grid definition in code behind -->
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我试过类似的东西
this.Items.ItemsPanel.Template = new Grid();
Run Code Online (Sandbox Code Playgroud)
但悲惨地失败了.有帮助吗?
背景: 我只知道运行时网格列和行的数量.
我正在尝试创建一个ListViewwith grouping,其中每个组中的元素都是水平显示的(作为可滚动内容).无论我尝试了什么GroupStyle.Panel,ListView它似乎没有对列表有任何影响.
以下是我的XAML的外观:
<ListView x:Name="itemListView"
Padding="10"
SelectionMode="None"
IsSwipeEnabled="False"
IsItemClickEnabled="True"
ItemTemplate="{StaticResource listItemTemplate}">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<ItemsWrapGrid ItemWidth="144" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GroupStyle.Panel>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding DisplayTitle}"
Margin="0,10,0,5"
Foreground="Black"
Style="{StaticResource SubheaderTextBlockStyle}"
TextWrapping="NoWrap" />
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
Run Code Online (Sandbox Code Playgroud)
哪里
<Page.Resources>
<DataTemplate x:Key="listItemTemplate">
<Grid Width="144" Margin="5">
<!-- details -->
</Grid>
</DataTemplate>
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)
下图显示了我得到的实际结果,右边显示了我想要的结果.

我尝试使用ItemsWrapGrid具有不同属性的a,我尝试过a StackPanel甚至是a VariableSizedWrapGrid,但是列表项的显示方式没有任何改变.
如何才能做到这一点?
xaml listview groupstyle itemspaneltemplate windows-phone-8.1
我有一个列表框,ItemsPanelTemplate设置为UniformGrid,行= 6,cols = 7.
我希望列表框项目填补他们的空间.
我正在使用字典中定义的数据完成.
我的模板的外部控件是一个Border,HorizontalAlignment = Stretch和VerticalAlignent = Strectch,但模板没有填充列表框项空间?
有任何想法吗?马尔科姆
我有一个列表框,使用GroupStyle对项目进行分组.我想在stackpanel的底部添加一个控件来保存所有组.此附加控件需要是滚动内容的一部分,以便用户滚动到列表的底部以查看控件.如果我使用的是没有组的列表框,则通过修改ListBox模板可以轻松完成此任务.但是,对于分组的项目,ListBox模板似乎仅适用于每个组.我可以修改GroupStyle.Panel,但这不允许我向该面板添加项目.
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/> **<----- I would like to add a control to this stackpanel**
</ItemsPanelTemplate>
</GroupStyle.Panel>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Grid>
<ItemsPresenter />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
Run Code Online (Sandbox Code Playgroud)
这应该让我知道我需要做什么:

我正在按照这个答案实现一个ListBox,其ItemsPanel是一个WrapPanel ,但有一个转折:我的ItemsSource是一个分组的 CollectionView.随着GroupStyle应用到我的列表框,在这个问题出在包装不起作用:该组总是垂直显示.
窥探我的应用程序,原因如下:

正如你所看到的,WrapPanel,定义为我的列表框的ItemsPanelTemplate,出现在ItemsPresenter 内各GroupItem; 创建一个隐式的,垂直方向的StackPanel(粉红色框中的顶部项)以包含GroupItems本身.
有没有办法覆盖这种行为,所以GroupItems放在一个WrapPanel?我是否需要重新模板化整个ListBox?
更新:为了说明我对ListBox和CollectionView分组的实际操作,让我发布一个小XAML:
<Grid>
<ListBox ItemsSource="{Binding}"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
SelectionMode="Multiple"
ItemContainerStyle="{StaticResource itemStyle}">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontWeight="Bold"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type WpfApplication1:Item}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}" FontSize="10"/>
<TextBlock Text="{Binding Amount, StringFormat={}{0:C}}" FontSize="10"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
它GroupStyle是它的核心:如果你删除它,GroupItems不会被渲染,并且WrapPanel(你可以在上面的截图中看到出现在GroupItem下面)出现在屏幕截图中的(StackPanel)98的位置.
我已经使用了ItemsPanelTemplate其他控件,比如ListBox,所以我认为做同样的事情TabControl会很简单.
显然,我错过了一些东西而且TabControl完全无视我放置的内容ItemsPanelTemplate.
我有xaml,看起来有点像这样:
<TabControl TabStripPlacement="Right" ItemsSource="{Binding Components}">
<TabControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</TabControl.ItemsPanel>
</TabControl>
Run Code Online (Sandbox Code Playgroud)
我已经尝试更换WrapPanel,UniformGrid看看是否有差异,它的行为相同.我确实有一个ControlTemplatefor TabItem,但我尝试删除它并没有任何区别所以我认为这不会影响我的问题.
我想进行搜索ComboBox,搜索框TextBox显示ItemsPanel在ComboBox下拉列表展开的上方.我想我需要制作一个自定义控件才能实现搜索功能,但首先我只想尝试TextBox使用普通模式进行显示ComboBox.这是我当前的尝试,当我尝试扩展下拉列表时产生异常:
<Style x:Key="FilteredComboBox" TargetType="ComboBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel>
<TextBox/>
<StackPanel IsItemsHost="True"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</StackPanel>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
这产生的例外是:
无法显式修改用作ItemsControl的ItemsPanel的Panel的Children集合.ItemsControl为Panel生成子元素.
我很确定有办法让这样做我想要的,但经过几个小时的谷歌搜索和反复试验,我的脑袋现在正在旋转.任何帮助将不胜感激!
我正在构建一个二十一点程序,目前正在研究手牌显示。
我有一个 PlayerSeat UserControl,其中有一个 ItemsControl 用于显示卡片。与正常的二十一点游戏一样,纸牌(在我的例子中是图像)是一张一张地叠在一起的。不同的游戏动作(拆分、双倍下注等)必须改变屏幕上纸牌的布局。ItemsControl 的 ItemSource 属性是ObservableCollection<Card> Hand我的 Player 的 ViewModel 中的“”属性。Card 对象包含带有 Card 图像的 BitmapSource。
我浏览了几个网页(参见帖子末尾),寻找实现我想要的目标的方法。我正在寻找一种方法来执行两个选项之一。
(首选)指定每个“手模式”(拆分、双下等)的布局,并指定手 ( OC<Card>) 的每个索引应按顺序放置的位置。例如,对于 Hand 中的第一张卡片,将 Source 绑定到 Hand[0].CardImage 的 Image 控件放在 (X1, Y1) 处,然后将 Hand[1] Image 放在 (X2, Y2) 处,依此类推。最好可以通过在 ItemsControl 上设置某种绑定模板属性(以在手部模式之间进行更改)来进行调整。
(后备)显示所有已绑定 Source 属性的 Image 控件。将这些图像的 Top/Left 属性绑定到 Hand[0].Top/Left 并在 Hand 类中进行位置计算。
我不是一个在没有亲自调查这个问题的情况下就提出问题的人。看来我需要将 ItemsPanelTemplate 与 StackPanel 一起使用,但不知道从哪里开始。关键是使图像重叠并放置在我想要的位置。您对我的问题的任何启发都会有所帮助。
参考: http: //drwpf.com/blog/itemscontrol-a-to-z/(特别是“ItemsControl:'P'代表面板”)
wpf itemscontrol observablecollection stackpanel itemspaneltemplate
wpf ×12
listbox ×4
xaml ×4
itemscontrol ×3
groupstyle ×2
.net ×1
.net-4.0 ×1
c# ×1
combobox ×1
data-binding ×1
grid ×1
grouping ×1
itemspanel ×1
itemtemplate ×1
listview ×1
stackpanel ×1
tabcontrol ×1
textbox ×1
wrappanel ×1