【发布时间】:2011-05-03 16:58:26
【问题描述】:
我想写一个 ItemsControl 派生的自定义控件。这部分是出于需要,部分是作为学习练习 - 请不要建议 I Style、DataTemplate、ControlTemplate 和 ListBox 等......即请不要质疑需求 - 只需假设它是真实的。
我浏览了网络,发现了很多有用的 ItemControl 信息,但没有明确的例子。
当我在 VS 中创建新的自定义控件时,我得到了几乎空的代码和带有<Style> 块的 Generic.xaml,它可以通过<Setter Property="Template"> 等设置 ControlTemplates、DataTemplates、Presenter 等。但是最小的 xaml/code 是多少需要在这里获得一个控件,该控件将绑定到一个 ObservableCollection 到 ItemsSoruce 以显示为一个列表? 换句话说:ItemsControl 派生的自定义控件的规范形式是什么?
我需要 ItemsPresenter 吗?是否必须在 ControlTemplate 中指定堆栈面板?我必须在<Setter Property="ItemTemplate"> 上设置 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>
【问题讨论】:
标签: wpf custom-controls itemscontrol