【问题标题】:How to draw a border around each element in ItemsControl?如何在 ItemsControl 中的每个元素周围绘制边框?
【发布时间】:2012-08-04 19:39:31
【问题描述】:
我正在使用 ItemsControl 和 ItemTemplateSelector 来绘制显示我的项目的 UI。但是现在,我的所有元素都需要在 Grid 中(并且元素应该在其中的一列)。
一开始我认为将ContentControl 嵌套在ItemsControl 中是正确的,而这个ContentControl 应该有ItemTemplateSelector,但我不确定这是否是最好的方法.
【问题讨论】:
标签:
c#
wpf
itemscontrol
itemtemplate
itemtemplateselector
【解决方案1】:
类似的东西应该在项目容器的Template 中,因为ItemsControls 有点问题,因为容器是ContentPresenters,没有Template。您可以将ItemsControl to use a ContentControl 子类化,然后使用ItemsControl.ItemContainerStyle 编辑这些容器的Template。
【解决方案2】:
<ItemsControl x:Name="lst">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="10" CornerRadius="1" BorderBrush="Navy">
<TextBox Text="{Binding Name}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我希望这会有所帮助。