【问题标题】:WPF - Hosting a content control inside a Data Template via codeWPF - 通过代码在数据模板中托管内容控件
【发布时间】:2013-02-21 03:38:36
【问题描述】:

我正在尝试在数据模板中托管内容控件。

与此完全相似: Putting a ContentControl *inside* a WPF DataTemplate?

我通过 XAML 成功地做到了。我想通过代码做同样的事情。

我创建了一个样式:

<Style x:Key="radioButtonAddtruefalse">
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate>
        <StackPanel Orientation="Horizontal">
          <RadioButton Content="True"  IsChecked="{Binding Value}"></RadioButton>
          <RadioButton Content="False" IsChecked="{Binding Value, Converter={StaticResource _invertedBooleanConverter}}"></RadioButton>
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

在数据模板中:

                <DataTemplate>
                  <ContentControl Style="{StaticResource radioButtonAddtruefalse}"> /ContentControl>
                </DataTemplate>

我尝试通过代码执行此操作,但在 DataTemplate 下没有发现任何允许我托管内容控件的内容。有什么建议吗?

【问题讨论】:

标签: c# wpf wpf-controls


【解决方案1】:

刚刚从MSDN Forum 复制,但这应该可以。不过没试过。

FrameworkElementFactory fef = new FrameworkElementFactory(typeof(TextBlock));

Binding placeBinding = new Binding();

fef.SetBinding(TextBlock.TextProperty, placeBinding);

placeBinding.Path = new PropertyPath("Name");

dataTemplate = new DataTemplate();

dataTemplate.VisualTree = fef;

也看看 Create DataTemplate in code behind

【讨论】:

  • 感谢您的回复。原来在 xaml 中完成的 contentcontrol 并没有真正出现在模板的可视化树中。小时候就在那里...找到了解决方法。 (不特定于上面的讨论,因此不把它放进去)。不过,我确实了解了 FrameworkElementFactory。谢谢。
猜你喜欢
  • 2012-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多