2009-08-21 11 views
0

ControlTemplateを初めて使用しています。私は最初のコントロールを書いていますが、私は(私にとってはそう思われる)非常に奇妙な問題を抱えています。.NET FrameworkオブジェクトでTemplateBindingが動作しない

私はTemplateBindingは仕事に行ったすべての依存関係プロパティが、.NETフレームワークは、すなわちContentControlContent財産やItemsControlItemsプロパティオブジェクトからすべてのプロパティは、実行時に読み込まれません。

私は何か...それは私が知らないだけで何をしないのです確信しています...

コードの例は以下の通りです:

クラスは、現時点では非常に簡単です:

public class Title : ContentControl 
{ 
} 

、テンプレートは次のとおりです。

<Style TargetType="{x:Type UI:Title}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type UI:Title}"> 
       <TextBlock Text="{TemplateBinding Content}" /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

ベースContentControl CL assはSystem.Windows.Controls.Control名前空間にある.NETクラスです。

おかげで、

アダム

+0

何がうまくいかないかを示すためにコードスニペットを追加できますか? – Bubblewrap

+0

申し訳ありません。私は今それを追加しました。 ありがとうございます。 – Adam

答えて

1

Contentが配置されている場所を上書きしたい場合は、ContentPresenterを使用してそれを行うことができます。私はTextBlock.TextプロパティがContentControl.Contentからすべてを受け入れないだろうと考えているように私はまた、ラベルにのTextBlockから変更した

<Style TargetType="{x:Type UI:Title}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type UI:Title}"> 
       <Label> 
        <ContentPresenter /> 
       </Label> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

注意。ここに私がまとめた例を示します。

<Window x:Class="ContentControlTest.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:ContentControlTest" 
     Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <Style TargetType="{x:Type local:Title}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type local:Title}"> 
         <Button> 
          <ContentPresenter /> 
         </Button> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 
    <local:Title> 
     <TextBlock Text="Happy Days!" /> 
    </local:Title> 
</Window> 
+0

私はちょうどあなたの提案を試みました。それは動作していないようです...私はもう少し調査を行って、それは表示されませんプロパティを設定するに関係なく、私は値を割り当てるタイトルクラスのコンストラクタに値を配置するようだコンテンツのプロパティは、それが表示されます... – Adam

+0

任意のアイデア?ご意見ありがとうございます。 – Adam

+0

実例を更新しました。 – user7116

0

あなたのコレクションにあなたのオブジェクトとINotifyCollectionChangedにINotifyPropertyChangedのインターフェイスを実装する必要があるかもしれません。

関連する問題