2011-01-09 6 views
17

次のコントロールテンプレートがあります。コントロールテンプレートのテンプレートバインディング

コントロール テンプレートのイメージコントロールのソースプロパティをテンプレートバインディングを使用して設定したいとします。

しかし、これはボタンコントロールのコントロールテンプレートであり、ボタンコントロールは ソースプロパティを持たないため、この場合はTemplateBindingを使用できません。

<ControlTemplate x:Key="BtnTemplate" TargetType="Button"> 
     <Border CornerRadius="5" Margin="15" Cursor="Hand"> 
      <StackPanel> 
       <Image Name="Img" Style="{StaticResource ImageStyle}" Source="temp.jpg" Height="100" Width="100" Margin="5"></Image> 
       <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label> 
      </StackPanel> 
     </Border> 
    </ControlTemplate> 

ボタンのインスタンスごとに異なるイメージを設定する必要があるため、パスもハードコードできません。

このような状況に対処する方法を教えてください。

答えて

28

を私は、例えば、動的なリソースを使用することをお勧めしたいです次のようにテンプレートを定義します。

<ControlTemplate x:Key="buttonTemplate" TargetType="Button"> 
    <Border CornerRadius="5" Margin="15" Cursor="Hand"> 
     <StackPanel Orientation="Horizontal" Background="Yellow"> 
      <Image Source="{DynamicResource ResourceKey=Img}" Height="100" Width="100" Margin="5"></Image> 
      <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label> 
     </StackPanel> 
    </Border> 
</ControlTemplate> 

そして、このようにそれを使用します。

<Button Content="Button" Template="{StaticResource ResourceKey=buttonTemplate}"> 
    <Button.Resources> 
     <ImageSource x:Key="Img">SomeUri.png/</ImageSource> 
    </Button.Resources> 
</Button> 
+1

ボタンをもっと簡潔に使用することはできますか? のように? 私はこれに打たれました、あなたは私を助けてくれますか? http://stackoverflow.com/q/13464735/640607 – Shankar

2

ボタンの消費者がどのようにソースを設定すると思われるかは、実際には述べていません。たとえば、Button.Tagプロパティを使用して、テンプレート内のプロパティにバインドすることができます。それとも、独自のコントロール定義することができます。

public class ImageButton : Button 
{ 
    // add Source dependency property a la Image 
} 

をそしてテンプレート:

<ControlTemplate TargetType="ImageButton"> 
    <Border CornerRadius="5" Margin="15" Cursor="Hand"> 
     <StackPanel> 
      <Image Name="Img" Style="{StaticResource ImageStyle}" Source="{TempateBinding Source}" Height="100" Width="100" Margin="5"></Image> 
      <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label> 
     </StackPanel> 
    </Border> 
</ControlTemplate> 
+0

私はタグプロパティを使用しましたが、動作しません。 ソース= "{TemplateBindingタグ}"ボタンを作成中に、Tag = "Temp.jpg"しかし、これは機能しません。 コードビハインドなしでXAMLでソースを設定したいと思います。また、オブジェクトの作成時にいくつかのプロパティを設定するコントロールテンプレート内に多数のコントロールが存在する可能性があるので、Tagは良い考えではないかもしれません。 – Sam

+0

'Source'はタイプ' ImageSource'であり、 "Temp.jpg"は 'string'であるため動作しません。 'Image'コントロールのように、文字列をImageSourceに変換するためにコンバーターを使う必要があります。 –

5

TemplateBindingのは軽量な「結合」、それは、このような自動的に入力するように、伝統的なバインディングの一部の機能をサポートしていないです(文字列URIをBitmapSourceインスタンスに変換するなど)対象のプロパティに関連付けられている既知の型コンバータを使用して変換します。

次のコードが正常に動作することができます:

<Window x:Class="GridScroll.Window2" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window2"> 
<Window.Resources> 
    <Style TargetType="{x:Type Button}" x:Key="ButtonStyle"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Border CornerRadius="5" Margin="15" Cursor="Hand" Background="Red"> 
         <StackPanel Orientation="Horizontal" Background="White"> 
          <Image Name="Img" Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Margin="5"></Image> 
          <Label Content="{TemplateBinding Content}" Margin="2"></Label> 
         </StackPanel> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 


</Window.Resources> 
<StackPanel Orientation="Horizontal"> 
    <Button Style="{StaticResource ButtonStyle}" Tag="a.jpeg" Content="a"/> 
    <Button Style="{StaticResource ButtonStyle}" Tag="b.png" Content="b"/> 
</StackPanel> 

+0

には、タグを使用するよりも、他の方法があります。私は心配している、後で私は私のコントロールテンプレートで複数のコントロールを持つ可能性があり、私はオブジェクトを作成するときにプロパティを設定したいと思います。しかし、タグはすでに使用されているので、私は再びそれを使用することはできません。 – Sam

+0

あなたはボタンからダイレクトして独自のコントロールを作成することができます。カスタム依存関係プロパティを追加して、私が使用するタグとして使用します – biju

+0

私の問題は元の投稿とは少し異なりますが、これはトリックを行った唯一の解決策でした。単一のリソースディクショナリにあるエラーが、私たちのアプリケーションを通してUIがいくつかのxamlデザイナーに読み込まれるのを妨げていたので、私は今朝これを費やしました。真剣に、ありがとう。 – CodeOtaku

1

を私は非常によくあなたの問題を理解していることはよく分からないが、なぜあなたはのContentPresenterを使用していませんか?画像のコードをより高いレベルで移動することができます。

<ControlTemplate x:Key="BtnTemplate" TargetType="Button"> 
    ... 
    <ContentPresenter/> 
</ControlTemplate> 
... 
<Button Template="{StaticResource BtnTemplate}"> 
    <Image .../> 
</Button> 
関連する問題