- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个用户控件,其中包含一个带有自定义 ItemsPanelTemplate 的 ListView。
<UserControl x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="thisUserControl">
<ListView ItemsSource="{Binding Source={StaticResource cvs}}"
Name="mainListView">
<ListView.GroupStyle>
<GroupStyle>
...
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<cd:TimeLinePanel UnitsPerSecond="{Binding ElementName=thisUserControl,Path=DataContext.UnitsPerSecond}" Start="{Binding ElementName=thisUserControl, Path=DataContext.Start}"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
UserControl 的 DataContext 有两个属性 Start 和 UnitsPerSecond。当我使用组时,我不能简单地写
Start={Binding Path=.Start}
因此我使用了上面的代码。但是,如果我将 Start 的绑定(bind)更改为此,我会得到一个异常:
ItemsPanelTemplate 的 VisualTree 必须是单个元素。
显然,ItemsPanelTemplate 只有一个元素。
那么可能是什么问题呢?我的自定义面板不创建任何元素。它只是安排它们。
最佳答案
您收到此异常可能是因为您试图将“Children”添加到 TimeLinePanel(或者您正在覆盖面板的可视化树并在“VisualChildrenCount”中返回 1 以外的内容)。遗憾的是,由于 ItemsPanelTemplate,如果面板是在 ItemsControl 中创建的,则您无法修改面板的“Children”属性。在 ItemsControl 之外,没有问题。
或者,您可以将 ListView 的模板覆盖为如下所示的内容 - 它适用于我的情况,并消除了异常。
<ListView.Template>
<ControlTemplate>
<cd:TimeLinePanel IsItemsHost="True" UnitsPerSecond="..."/>
<ControlTemplate>
</ListView.ItemsPanel>
有一篇 CodeProject 文章 ( http://www.codeproject.com/KB/WPF/ConceptualChildren.aspx ) 提供了有关此行为的一些详细信息,这可能有助于您理解为什么会这样。
关于wpf - ListView 的 ItemsPanelTemplate 显然错误地抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1343205/
我需要设置 ItemsPanelTemplate ListBox 的属性基于 DependencyProperty在控件上。我如何使用 DataTemplateSelector要做到这一点? 我有类似
如果我想将 ItemsPanelTemplate 从默认 (StackPanel) 更改为 Grid
我有一个 ListView,它的布局看起来像一个 Windows 资源管理器 View (图标 + 一些细节),绑定(bind)到 ViewModel 中某处的列表。 我的目标是能够随时在资源管理器
我正在 Silverlight 中构建一个自定义 ItemsControl(除其他外),它允许项目在运行时水平或垂直显示。如何将 ItemsPanel 的 Orientation 属性绑定(bind)
我在 XAML 中定义了这个 UserControl 并且想在我的代码隐藏类中动态设置 ItemsPanelTemplate(不在XAML 就像示例中一样):
我创建了一个用户控件,其中包含一个带有自定义 ItemsPanelTemplate 的 ListView。 ...
我用过 ItemsPanelTemplate在其他控件上,例如 ListBox ,所以我想对 TabControl 做同样的事情会很简单。 显然,我遗漏了一些东西和 TabControl完全无视我放在
我想在 WPF 中添加一个 Grid 作为 ItemsControl 的 ItemPanelTemplate。 我知道有很多类似的问题。但到目前为止,我所看到的所有这些都在谈论动态添加行和列,但它们并
我想在运行时更改 ListBox 的 ItemsPanelTemplate。 我有以下 XAML,它允许我更改 ItemsPanelTemplate;但是有破坏 ScrollViewer 的不良副作用
我需要根据控件的依赖属性设置列表框的 ItemsPanelTemplate 属性。我如何使用 DataTemplateSelector 来做到这一点? 我有这样的东西:
我正在尝试覆盖 Silverlight 中 TabControl 的默认模板。我不想让标签在它们满时环绕,而是想让用户可以滚动浏览它们,类似于 ListBox。但是,Silverlight 会忽略我在
我正在处理一个动态创建的网格,在每个新列中包含一个添加到 ItemsControl 的新项目。 我正在使用 Rachel Lim 的 GridHelper 现在,我有一个如下所示的主窗口, Xaml
我正在尝试创建与组合框相关的混合行为。为了获得我想要的效果,ComboBox 的 ItemsPanel 必须添加特定的元素。我不想在每个使用该行为的 ComboBox 中都这样做,所以我希望 Beha
那么如果我有一个items控件,想在后面的代码中获取 Canvas ,我该怎么办?我可以获得 itemscontrol,但是我该怎么做才能检索 Canvas ?我试过了 Canvas c1 = ic.
当 ItemsControl 被 ItemsSource 绑定(bind)时,我正在尝试访问 DataTemplate 内的 Grid。 这是完整的 XMAL 代码,我如何从外部找到某个元素? for
我正在尝试像这样修改我的 WrapGrid 的 MaximumRowsOrColumns 属性: 然后我使用此代码更改 WrapGrid:
我一直在开发我的第一个 MVVM WPF 应用程序,我想在其中绘制一个包含节点和边的图形。目前,我正在 View 后面的代码中执行所有绘图逻辑,迭代节点,相应地创建形状并将它们添加到 Canvas 中
我使用 ListBox 在我的 View 中显示 ObservableCollection 的内容。在我的简化示例中,我只是为每个项目使用一个图像,如下所示:
我有一个自定义面板,我在其中声明了一个自定义属性来保存内容(我不想为内容使用子项): [ContentProperty(Name = "PanelContent")] public class Cus
我是一名优秀的程序员,十分优秀!