7

WPF: Several content presenters in a custom control?

 2 years ago
source link: https://www.codesd.com/item/wpf-several-content-presenters-in-a-custom-control.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

WPF: Several content presenters in a custom control?

advertisements

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?


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}"/>

The control itself should expose two properties for content and set the default using the ContentPropertyAttribute:

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK