【发布时间】:2016-04-18 13:34:06
【问题描述】:
在纸牌游戏中,我需要一个在网格中显示 4 个项目(北、东、南和西)的集合的控件。网格为 3x3,分别显示单元格 (0,1) (1,0) (1,2) 和 (2,1) 中的内容。
<bridge:SeatCollection ItemsSource="{Binding Path=Hands}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</bridge:SeatCollection>
上述我已经开始工作,并且通过创建一个从 ItemsControl 继承的控件并在 GetContainerForItemOverride 中执行一些智能操作来完成,如 StackOverflow 上的另一篇文章所述。
我的问题是如何将另一个属性添加到包含必须在中间单元格中显示的内容的控件以及控件如何呈现该内容?
使用控件应该是这样的:
<bridge:SeatCollection ItemsSource="{Binding Path=Hands}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<bridge:SeatCollection.MiddleCell>
<TextBlock Text="Content for middle cell"/>
</bridge:SeatCollection.MiddleCell>
</bridge:SeatCollection>
根据这篇文章 (http://www.ikriv.com/dev/wpf/displayingcontent/index.html),我可以添加一个属性并在 ContentPresenter 中使用该属性
public object MiddleCell
{
get { return (object)GetValue(MiddleContentProperty); }
set { SetValue(MiddleContentProperty, value); }
}
public static readonly DependencyProperty MiddleContentProperty =
DependencyProperty.Register("MiddleCell", typeof(object), typeof(SeatCollection), new PropertyMetadata(0));
但是我可以在哪里以及如何在代码中将此内容添加到网格的中间单元格?
【问题讨论】:
-
您是否尝试过使用 ItemTemplateSelector 属性? msdn.microsoft.com/en-us/library/…
-
我意识到我过分强调模板的使用。我只需要将内容添加到网格的中间单元格。