简体   繁体   English

WPF ContentPresenter在ItemsControl.ItemTemplate内部

[英]WPF ContentPresenter inside an ItemsControl.ItemTemplate

I have two user controls, WorkflowTileItemsControl and WorkflowTileControl. 我有两个用户控件,WorkflowTileItemsControl和WorkflowTileControl。 The WorkflowTileItemsControl hosts the WorkflowTileControl in an ItemsControl. WorkflowTileItemsControl将WorkflowTileControl托管在ItemsControl中。 However, there are dependency properties on the WorkflowTileControl that I would like to expose to anything using the WorkflowTileItemsControl. 但是,我想使用WorkflowTileItemsControl暴露给WorkflowTileControl的依赖项属性。 In order to do that here is ItemsControl code for WorkflowTileItemsControl. 为此,这里是WorkflowTileItemsControl的ItemsControl代码。

<ItemsControl ItemsSource="{Binding Source={StaticResource WorkflowTilesViewSource}}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <ContentPresenter Content="{Binding WorkflowTileControl, ElementName=ctrlWorkflowTileItems}" />
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

However this doesn't seem to work. 但是,这似乎不起作用。 It is only showing the last item in the ItemsControl. 它仅显示ItemsControl中的最后一项。 Below is code that works, and is the functionality I'm looking for (minus hard coding all the dependency properties). 下面是有效的代码,也是我要寻找的功能(减去对所有依赖项属性进行硬编码)。

<ItemsControl ItemsSource="{Binding Source={StaticResource WorkflowTilesViewSource}}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <OrderCommon:WorkflowTileControl IsReadOnly="True" Margin="5" TasksTitle="Defects" />
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

And this is what my calling code looks like. 这就是我的调用代码的样子。

<OrderCommon:WorkflowTileItemsControl WorkflowRequirementTypeCode="DISBURSEMENTDFCT" Margin="5" MinWidth="1000" MaxWidth="1250" HorizontalAlignment="Left">
  <OrderCommon:WorkflowTileItemsControl.WorkflowTileControl>
    <OrderCommon:WorkflowTileControl IsReadOnly="True" Margin="5" TasksTitle="Defects" />
  </OrderCommon:WorkflowTileItemsControl.WorkflowTileControl>
</OrderCommon:WorkflowTileItemsControl>

I feel like there is some simple step I'm missing. 我觉得我缺少一些简单的步骤。 I'm not sure if ContentPresenter is the right tool for the job. 我不确定ContentPresenter是否适合这项工作。 I haven't done anything like this in WPF before. 我以前在WPF中没有做过这样的事情。 Can someone please assist? 有人可以帮忙吗?

So after some days of research I've found a solution. 因此,经过几天的研究,我找到了解决方案。 The WorkflowTileItemsControl needs to expose a Dependency Property of a DataTemplate which will be bound to the ItemsTemplate for the ItemsControl. WorkflowTileItemsControl需要公开一个DataTemplate的Dependency属性,该属性将绑定到ItemsControl的ItemsTemplate。 Here is the xaml for the WorkflowTileItemsControl: 这是WorkflowTileItemsControl的xaml:

<ItemsControl ItemsSource="{Binding Source={StaticResource WorkflowTilesViewSource}}" ItemTemplate="{Binding WorkflowTileTemplate, ElementName=ctrlWorkflowTileItems}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

And Here is the xaml for the calling control: 这是调用控件的xaml:

<OrderCommon:WorkflowTileItemsControl WorkflowRequirementTypeCode="DISBURSEMENTDFCT" Margin="5" Width="1130" HorizontalAlignment="Left">
  <OrderCommon:WorkflowTileItemsControl.WorkflowTileTemplate>
    <DataTemplate>
      <OrderCommon:WorkflowTileControl IsReadOnly="True" Margin="5" TasksTitle="Defects" />
    </DataTemplate>
  </OrderCommon:WorkflowTileItemsControl.WorkflowTileTemplate>
</OrderCommon:WorkflowTileItemsControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM