2016-09-15 25 views
1

ThumbコントロールのDragDeltaを使用してドラッグできるカスタムコントロールを作成しました。私は、カスタムコントロールContentPresenter内に図形、画像またはテキストブロックを挿入できるようにしたい。カスタムコントロール(Thumb)にContentPresenterを使用

CustomControl.xaml(親指)

<Thumb x:Class="StackOverflow.CustomControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Thumb.Template> 
     <ControlTemplate> 
      <ContentPresenter/> 
     </ControlTemplate> 
    </Thumb.Template> 
</Thumb> 

MainWindow.xaml

<Window x:Class="StackOverflow.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:StackOverflow"> 
    <local:CustomControl> 
     <!--Shape, Image or TextBlock--> 
    </local:CustomControl> 
</Window> 
+0

Thumb'は、私はあなたが持っているつもりだと思う、ContentControl' 'から派生していないので'あなたのクラスに 'Object'型の' Content'依存性プロパティを与え、カスタムコントロールクラスに 'ContentProperty(" Content ")[DefaultProperty(" Content ")]'という2つの属性を与えます。 –

+0

...あなたの親クラスが実際に呼び出されたものであれば、 'ControlTemplate'' TargetType =" local:CustomControl "'を与えます。 –

答えて

1

、あなたはContentControlに行くそこには何も置くことができます:ビジュアルツリー要素、文字列、この特定のケースでは、暗黙のDataTemplate(かなりこじつけでのviewmodelを、それが原則ですものの名前) - あなたはそれを名づけます。 ControlTemplateTargetTypeが一致しないため、

MyThumb.xaml

<Thumb 
    x:Class="ThumbTest.MyThumb" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:thumb="clr-namespace:ThumbTest" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="300" 
    > 
    <Thumb.Template> 
     <ControlTemplate TargetType="thumb:MyThumb"> 
      <ContentPresenter 
       /> 
     </ControlTemplate> 
    </Thumb.Template> 
</Thumb> 

VSは私のXAMLで<Thumb...の下に青い波線を与えているが、それは構築し、正常に動作します。テンプレートへのこの変更は、その取り除くます:

<Thumb.Template> 
    <ControlTemplate TargetType="thumb:MyThumb"> 
     <ContentControl 
      Content="{Binding Content, RelativeSource={RelativeSource AncestorType=thumb:MyThumb}}" 
      /> 
    </ControlTemplate> 
</Thumb.Template> 

MyThumb.xaml.cs

using System; 
using System.ComponentModel; 
using System.Windows; 
using System.Windows.Controls.Primitives; 
using System.Windows.Markup; 

namespace ThumbTest 
{ 
    [ContentProperty("Content")] 
    public partial class MyThumb : Thumb 
    { 
     public MyThumb() 
     { 
      InitializeComponent(); 
     } 

     #region Content Property 
     public Object Content 
     { 
      get { return (Object)GetValue(ContentProperty); } 
      set { SetValue(ContentProperty, value); } 
     } 

     public static readonly DependencyProperty ContentProperty = 
      DependencyProperty.Register("Content", typeof(Object), typeof(MyThumb), 
       new PropertyMetadata(null)); 
     #endregion Content Property 
    } 
} 
0

クレジットは、エド・プランケット

CustomControl.xaml.cs(親指)に行く

プロパティが Objectあるので
[ContentProperty("Content")] 
public partial class CustomControl : Thumb 
{ 
    public CustomControl() 
    { 
     InitializeComponent(); 
    } 

    public FrameworkElement Content { get; set; } 
} 
+0

これを 'FrameworkElement'にする必要はありません。 'Object'はビジュアルツリー要素でも機能しますが、暗黙的なDataTemplatesで文字列やビューモデルを作成することもできます。 –

関連する問題