为 ItemsControl.ItemContainerStyle 指定 ControlTemplate
以下内容与我想要完成的任务类似。但是,我收到错误
PropertyDescriptor 值无效。
在模板 Setter
上。我怀疑这是因为我没有为Style
指定TargetType
;但是,我不知道 ItemsControl
的容器类型。
<ItemsControl>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<TextBlock Text="Some Content Here" />
<ContentPresenter />
<Button Content="Edit" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
<!-- heterogenous controls -->
<ItemsControl.Items>
<Button Content="Content 1" />
<TextBox Text="Content 2" />
<Label Content="Content 3" />
</ItemsControl.Items>
</ItemsControl>
The following is similar to what I'm trying to accomplish. However, I get the error
Invalid PropertyDescriptor value.
on the Template Setter
. I suspect it's because I didn't specify a TargetType
for the Style
; however, I don't know the container type for ItemsControl
.
<ItemsControl>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<TextBlock Text="Some Content Here" />
<ContentPresenter />
<Button Content="Edit" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
<!-- heterogenous controls -->
<ItemsControl.Items>
<Button Content="Content 1" />
<TextBox Text="Content 2" />
<Label Content="Content 3" />
</ItemsControl.Items>
</ItemsControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用类型名称限定属性名称:
ItemsControl
的容器通常是ContentPresenter
,但如果子项是UIElement
,则它不会使用容器。在本例中,所有子级都是 Control,因此ItemContainerStyle
将直接应用于它们。如果您添加了UIElement
以外的项目,该设置器将在ContentPresenter
上设置Control.Template
属性,该属性会成功,但不会影响。实际上,听起来您想要的是将每个子元素包装在一个容器中,即使它们已经是一个
UIElement
。为此,您必须使用ItemsControl
的子类。您可以使用现有的,例如ListBox
,也可以子类ItemsControl
并覆盖GetContainerForItemOverride
和IsItemItsOwnContainerOverride
将项目包装在您自己的容器中。您可以将它们包装在ContentControl
中,然后将其用作Style
的TargetType
。您还需要在
ControlTemplate
上设置TargetType
,以便ContentPresenter
绑定到Content
属性:You can qualify the property name with the type name:
The container for
ItemsControl
is normally aContentPresenter
, but if the child is aUIElement
then it won't use a container. In this case, all of the children are Controls, so theItemContainerStyle
will apply to them directly. If you added an item other than aUIElement
, that setter would set theControl.Template
property on theContentPresenter
, which would succeed but have no effect.Actually, it sounds like what you want is to wrap each child in a container, even if they are already a
UIElement
. To do that, you will have to use a subclass ofItemsControl
. You could use an existing one likeListBox
, or you could subclassItemsControl
and overrideGetContainerForItemOverride
andIsItemItsOwnContainerOverride
to wrap the items in your own container. You could wrap them in aContentControl
and then use that as theTargetType
for theStyle
.You will also need to set the
TargetType
on theControlTemplate
so that theContentPresenter
will bind to theContent
property:另外,如果您只想使用 XAML 完成所有操作,您可以简单地使用 ListBox 而不是 ItemsControl 并为 ListBoxItem 定义样式:
请注意,因为我使用 ListBox,所以容器是 ListBoxItem(通常 WPF 默认列表控件的容器始终命名为Item),因此我们为 ListBoxItem 创建一个样式:
然后我们为 ListBoxItem 创建一个新的 ControlTemplate。请注意,ContentPresenter 未使用,因为它总是出现在文章和教程中,您需要将其模板绑定到 ListBoxItem 的 Content 属性,以便它将显示该项目的内容。
我刚刚遇到了同样的问题并以这种方式解决了。我不想要 ListBox 的某些功能(项目选择),并且通过使用此技术,项目选择不再起作用。
Also if you only want to do all of it with XAML you can simply use ListBox instead of ItemsControl and define a style for ListBoxItem:
Note that because I am using ListBox the container is ListBoxItem(Generally the container for WPF's default list control is always named the Item) so we create a style for ListBoxItem:
Then we create a new ControlTemplate for ListBoxItem. Please note that ContentPresenter is not used as it always appears in articles and tutorials, you need to template-bind it to Content property of ListBoxItem, so it will show the content for that item.
I just had the same problem and fixed it this way. I dont wanted some functionalities of ListBox ( item selection ) and by using this technique the item selection does not work anymore.