【问题标题】:Bind dynamically generated Grid as DataTemplate to HubSection in C#将动态生成的 Grid 作为 DataTemplate 绑定到 C# 中的 HubSection
【发布时间】:2014-09-25 17:01:48
【问题描述】:

我为 Win 8.1 和 RT 编写了一个小应用程序。我想创建一个包含多个部分的集线器。在设计时,我不知道会有多少部分,也不知道它们的内容。所以我只是在设计时在 XAML 中创建了一个空的 Hub:

<Hub x:Name="MainHub" Grid.Row="2" Grid.Column ="1">
</Hub>

在运行时,我使用其复杂的内容创建我的 Hubsections。在 XAML 中我会这样写:

<Hub x:Name="MainHub" Grid.Row="2" Grid.Column ="1">
  <HubSection>
    <DataTemplate>
      <Grid Name="TemplateGrid">
        ...
      </Grid>
    </DataTemplate>
  </HubSection>
</Hub>

我已经在 C# 中完全创建了我的 TemplateGrid,现在我想将此网格用作 HubSection 的 DataTemplate(也是在 C# 中创建的),以最终将 Hubsection 添加到我的 Hub:

MainHub.Sections.Add(hs);

如何将我的 TemplateGrid 用作 DataTemplate。我找不到任何解决方案。 像这样简单的东西

hs.ContentTemplate = TemplateGrid as DataTemplate;

不工作。

有人有想法吗?

谢谢。

Cy

【问题讨论】:

    标签: c# xaml


    【解决方案1】:

    在 Page.Resources 中移动您的模板,并将 x:Key 设置为“TemplateGrid”

    <Page.Resources>
        <DataTemplate x:Key="TemplateGrid">
          <Grid>
            ...
          </Grid>
        </DataTemplate>
    </Page.Resources>
    

    在您的代码中:

    hs.ContentTemplate = this.Resources["TemplateGrid"] as DataTemplate;

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 2013-05-11
      • 2016-04-07
      相关资源
      最近更新 更多