WPF ItemsControl 派生自定义控件帮助
我想编写一个 ItemsControl 派生自定义控件。这部分是出于需要,部分是作为学习练习 - 请不要建议我使用 Style、DataTemplate、ControlTemplate、ListBox 等...即,请不要质疑这种需要 - 只要假设它是真实的。
我在网上搜索并发现了很多有用的 ItemControl 信息,但没有明确的示例。 当我在 VS 中创建新的自定义控件时,我得到了几乎空的代码和带有 块的 Generic.xaml,其中可以通过
<; 设置 ControlTemplates、DataTemplates、Presenters 等。但是,要获得一个控件,该控件将绑定到 ItemsSoruce 的 ObservableCollection 以显示为 列表? 换句话说:ItemsControl 派生的自定义控件的规范形式是什么?
我需要 ItemsPresenter 吗?我是否必须在 ControlTemplate 中指定堆栈面板?我是否必须在
上设置 TargetType?等等
勺子喂食更喜欢说:它很容易,我只需要将 DataTemplate 集成到项目控制容器的向量空间上,相对于面板演示者 yada yada...这不是一个很大的帮助。
更多信息:该控件仅面向显示,即没有所选项目等概念。
提前致谢!
默认 Generic.xaml(此处最少要添加什么?):
<Style TargetType="{x:Type local:MyItemsControlDerivedClass}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyItemsControlDerivedClass}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I would like to write a ItemsControl derived custom control. This is partially from need and partially as a learning exercise - please don't suggest I Style, DataTemplate, ControlTemplate a ListBox etc... I.e. please don't question the need - just assume its genuine.
I've trolled the web and found lots of useful ItemControl info but no clear cut examples.
When I create I new Custom Control in VS I get practically empty code behind and Generic.xaml with a <Style>
block where its possible to set ControlTemplates, DataTemplates, Presenters etc via <Setter Property="Template">
etc. But what is the minimum xaml/code needed here to get a control that will bind to an ObservableCollection to ItemsSoruce to display as a list? Put another way: whats the canoical form of a ItemsControl derived custom control?
Do I need an ItemsPresenter? Do I have to specify a stack pannel in the ControlTemplate? Do I have to set TargetType on the <Setter Property="ItemTemplate">
? etc.
Spoon feeding prefered eg saying: its easy and I just need to intergrate the DataTemplate over the vector space of item control containers with respect to the panel presenter yada yada... aint a great help.
Further info: The control is a display only orientated ie there is no concept of selected item etc.
Thanks in advance!
Default Generic.xaml (whats minimum to add here?):
<Style TargetType="{x:Type local:MyItemsControlDerivedClass}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyItemsControlDerivedClass}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需看一下默认样式(遵循
默认WPF 主题
链接):例如 ListBox:
Just take a look at the default styles (follow the
Default WPF Themes
link):e.g. ListBox:
按照 HB 建议添加默认样式即可工作并使控件可用(项目显示)。删除 ItemsPresenter(ScrollViewer 内)会破坏控件(无内容显示)。这篇文章解释了发生了什么:
http://drwpf.com/blog/2009/ 05/12/itemscontrol-l-is-for-lookless/
本质上,ControlTemplate 必须具有:
a) ItemPresenter 或
b) IsItemsHost 属性设置为 true 的面板。
也就是说,您需要添加到麦粒肿的最低限度是一个带有以下任一内容的 ControlTemplate:
或
Adding the default style as H.B suggests works and renders the control usable (items display). Removing the ItemsPresenter (inside the ScrollViewer) breaks the control (no content display). This post explains whats going on:
http://drwpf.com/blog/2009/05/12/itemscontrol-l-is-for-lookless/
Essentially the ControlTemplate must have either:
a) an ItemPresenter OR
b) a panel with IsItemsHost property set true.
I.e the bare minimum you need to add to the stye is a ControlTemplate with either:
<StackPanel IsItemsHost="True" />
OR
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>