开发者

Where should be put XAML for layout of custom control in WPF?

After custom control is created there automatically appeared file for C# code - MyCustomControl.cs:

public class MyCustomControl : ContentControl {
    static MyCustomControl( ) {
        ...
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl),
        new FrameworkPropertyMetadata(typeof(MyCustomControl)));
      }
        ...
 } 

and file for default stile - Themes\Generic.xaml:

 <!-- themes/generic.xaml -->
 <ResourceDictionary
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:CustomControlLib">
   <Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="Template">
     <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
       <Border Background="{TemplateBinding Background}"
               BorderBrush="{TemplateBinding BorderBrush}"
               BorderThickness="{TemplateBinding BorderThickness}">
         <ContentPresenter />
       </Border>
      </ControlTemplate>
     </Setter.Value>
    </Setter>
   </Style>
 </ResourceDictionary>

But where and how should I correctly place XAML code for layout开发者_如何转开发 and content of Custom Control itself?


The (default) layout and content of the custom control is defined by the ControlTemplate in generic.xaml. So you should put the layout and content within the ControlTemplate element that has been generated for you. (Note that the ContentPresenter will display content provided by users of your control: you need only to provide "content" that is part of your template, e.g. in a check box, your template would provide the little square but user content would provide the caption.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜