【问题标题】:Custom control with several datatemplates/contentpresenters具有多个数据模板/内容呈现器的自定义控件
【发布时间】: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/…
  • 我意识到我过分强调模板的使用。我只需要将内容添加到网格的中间单元格。

标签: wpf xaml


【解决方案1】:

我不知道您在 GetContainerForItemOverride 中做了什么聪明的事情,所以我将忽略这部分。您的 bridge:SeatCollection 是从 ItemsControl 派生的自定义控件,对吗?因此它将具有在 Themes\Generic.xaml 中定义的默认样式。更改该样式,使其具有 ContentPresenter,其中 ContentSource 设置为 MiddleCell(您的 DP)。像这样的:

<Style TargetType="{x:Type bridge:SeatCollection}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type bridge:SeatCollection}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <ContentPresenter ContentSource="MiddleCell" Grid.Row="1" Grid.Column="1" />

                        <!-- This is for the 'Hands' - Change as needed -->
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

【讨论】:

  • 我需要 MiddleCell 和手在同一个网格中。GetContainerForItemOverride 中的“聪明的东西”是为 Hands 中的每个项目设置 Grid.Row 和 Grid.Column。这不再适用于这种风格。而当我在Grid中添加IsItemsHost="True"时,不允许在Grid中放置ContentPresenter和ItemsPresenter。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-01
  • 2011-11-05
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多