c# - WPF Canvas 项和 DataTemplate

标签 c# wpf datatemplate

如果我有一个 DataTemplate(或类似的东西),我可以在 Canvas 中使用非 UIElements 吗?我觉得我以前做过这个,这是可能的,但我想不通。这是一些代码...

<Window x:Class="EntityTranslator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:EntityTranslator"
        Title="MainWindow" Height="350" Width="525">

  <Window.Resources>
    <local:Entity x:Key="DesignEntity}" EntityName="Test" />

    <DataTemplate DataType="{x:Type local:Entity}">
      <StackPanel>
        <TextBlock Text="{Binding Name}"/>
      </StackPanel>
    </DataTemplate>
  </Window.Resources>

    <Grid>
    <Canvas>
      <local:Entity EntityName="Test" />
    </Canvas>
  </Grid>
</Window>

最佳答案

将它们包裹在 ContentPresenter 中或 ContentControl ,它们是可以在其 Content

中承载任何对象类型的控件
<ContentPresenter>
    <local:Entity EntityName="Test" />
</ContentPresenter>

ContentPresenter 将使用您的隐式 DataTemplate 自动绘制项目

关于c# - WPF Canvas 项和 DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12865398/

相关文章:

wpf - 如何停止在特定条件下创建模板

Silverlight:按名称获取 DataTemplate 中的元素

c# - LINQ:获取父对象属性和单个子属性作为平面结构

c# - 在 instagram 中获取用户 ID

c# - 在 WPF 中绑定(bind)包含 WindowsFormHost 项的 ItemsControl

wpf - 在资源字典 wpf 中共享数据模板的一部分

wpf - PasswordBox 绑定(bind)问题 MVVM

c# - 我如何确保任何线程都不会无限期地等待?

c# - 什么决定了 "await"所花费的时间?

wpf - 您可以在 DataTemplate 中使用 CollectionViewSource 吗?