ItemTemplate:ListBox 与 ItemsControl
我对 WPF 世界还很陌生,在 ItemsControl 中模板化项目时遇到一些问题。我需要的是在 ItemsControl (或类似的)内模板化元素(主要是按钮)。
如果我使用以下 XAML 代码......
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<Border BorderBrush="AliceBlue"
BorderThickness="3">
<TextBlock Text="Templated!"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<Button>Not templated</Button>
<TextBlock>Text not templated</TextBlock>
</ItemsControl>
我得到这个结果: http ://img444.imageshack.us/img444/2167/itemscontrolnottemplate.gif
ItemsControl 未将模板应用于 Button 或 Button >TextBlock 控件。如果我将 ItemsControl 更改为 ListBox,如下所示:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<Border BorderBrush="AliceBlue"
BorderThickness="3">
<TextBlock Text="Templated!"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<Button>Not templated</Button>
<TextBlock>Text not templated</TextBlock>
</ListBox>
... 然后我得到以下结果: img814.imageshack.us/img814/6204/listboxtooomuchtemplatin.gif
现在该模板应用于两个子控件(即使我将DataType设置为Button)。
I'm quite new to the WPF world and I'm having some problems with templating items in an ItemsControl. What I need is to template elements (mostly buttons) inside an ItemsControl (or the like).
If I'm using the following XAML code ...
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<Border BorderBrush="AliceBlue"
BorderThickness="3">
<TextBlock Text="Templated!"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<Button>Not templated</Button>
<TextBlock>Text not templated</TextBlock>
</ItemsControl>
... I get this result: http://img444.imageshack.us/img444/2167/itemscontrolnottemplate.gif
The ItemsControl did not apply the template to either the Button nor to the TextBlock control. If I change the ItemsControl into a ListBox like this:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type Button}">
<Border BorderBrush="AliceBlue"
BorderThickness="3">
<TextBlock Text="Templated!"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<Button>Not templated</Button>
<TextBlock>Text not templated</TextBlock>
</ListBox>
... then I'm getting this result: img814.imageshack.us/img814/6204/listboxtoomuchtemplatin.gif
Now the template is applied to BOTH the child controls (even while I set the DataType to be Button only).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难推断您要做什么,但看看这是否有帮助...
如果它们已经是 UI 元素,普通的旧
ItemsControl
不会将其子项包装在容器中。另一方面,ListBox
要求将其子项包装在ListBoxItem
中。如果项目被包装,则将应用
ItemTemplate
。如果该项目未包装,则ItemTemplate
也可能不存在。您几乎总是希望将数据项添加到您的
ItemsControl
中,而不是UI元素。然后,您将DataTemplate
与这些数据元素相关联,以定义使用哪些 UI 元素来呈现它们。我认为解释你的最终目标对于进一步提供帮助是必要的。
It's difficult to infer what you're trying to do, but see if this helps...
A plain old
ItemsControl
won't wrap its children in a container if they're already UI elements. AListBox
, on the other hand, requires its children to be wrapped in aListBoxItem
.If the item is wrapped, then the
ItemTemplate
will be applied. If the item is not wrapped, theItemTemplate
may as well not exist.You almost always want to be adding data items to your
ItemsControl
, not UI elements. You then associateDataTemplate
s with those data elements to define what UI elements are used to render them.I think explaining your end goal would be necessary to help further.