WPF:在自定义控件的多个内容的主持人? [英] WPF: Multiple content presenters in a custom control?

查看:191
本文介绍了WPF:在自定义控件的多个内容的主持人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有需要XAML的2个或更多的区域由子控件来定义一个自定义的控制 - 从这个控件继承。我不知道是否有一种方法可以定义多个contentpresenters和一个充当默认的内容演示

I'm trying to have a custom control that requires 2 or more areas of the XAML to be defined by a child control - that inherits from this control. I'm wondering if there's a way to define multiple contentpresenters and one which acts as the default content presenter

<MyControl>
      <MyControl.MyContentPresenter2>
           <Button Content="I am inside the second content presenter!"/>
      </MyControl.MyContentPresenter2>

      <Button Content="I am inside default content presenter" />
</MyControl>



这是可能的,我怎么在自定义控件的模板定义此?

Is this possible, how do I define this in the custom control's template?

推荐答案

模板只需绑定像这样单独的 ContentPresenter 实例(我只能设置一个物业在这里,但你可能会想要设置别人):

The template can just bind the separate ContentPresenter instances like this (I've only set one property here but you'll likely want to set others):

<ContentPresenter Content="{TemplateBinding Content1}"/>
<ContentPresenter Content="{TemplateBinding Content2}"/>



控件本身应该公开内容两个属性和使用的 ContentPropertyAttribute

[ContentProperty("Content1")]
public class MyControl : Control
{
    // dependency properties for Content1 and Content2
    // you might also want Content1Template, Content2Template, Content1TemplateSelector, Content2TemplateSelector
}

这篇关于WPF:在自定义控件的多个内容的主持人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆