2016-08-25 1 views
1

私はカスタムボタンStyleXAMLで書いています。画像とテキストのボタンです。 しかし、Imageはカスタマイズ可能である必要があります。デザイナーのSourceプロパティを変更する必要があります。XAMLでリソーステンプレートの子値を変更するにはどうすればよいですか?

マイコード:

<Window.Resources> 
    <ResourceDictionary> 
    <Style x:Key="SSbutton" TargetType="{x:Type Button}"> 
      <Setter Property="Cursor" Value="Hand"/> 
      <Setter Property="Background" Value="Green"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Viewbox Stretch="Uniform"> 
          <Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" 
           Height="{TemplateBinding Height}"> 
           <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"> 
            <!--I want to change this Source property--> 
            <Image Source="img/desktop.png" Width="30" HorizontalAlignment="Left" /> 
            <TextBlock Margin="3,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center" 
              Text="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/> 
           </StackPanel> 
          </Border> 
         </Viewbox> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    </ResourceDictionary> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="25"/> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="90" /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <Border Grid.Column="0" Grid.Row="1" Background="LightGreen"> 
    <StackPanel > 
     <Button Style="{StaticResource SSbutton}" Width="90" Height="30" Content="Desktop" FontSize="13" 
       Foreground="White"/> 
    </StackPanel> 
    </Border> 
</Grid> 

私はそれをどのように行うことができますか?

答えて

1

バックハンディダンディTagプロパティとの結合、任意のテンプレートを使用してプロパティの中にピギー。

<Style x:Key="SSbutton" TargetType="{x:Type Button}"> 
      <Setter Property="Cursor" Value="Hand"/> 
      <Setter Property="Background" Value="Green"/> 
      <!-- Set a default --> 
      <Setter Property="Tag" Value="img/desktop.png"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Viewbox Stretch="Uniform"> 
          <Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" 
           Height="{TemplateBinding Height}"> 
           <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"> 
            <!--I want to change this Source property--> 
            <Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Width="30" HorizontalAlignment="Left" /> 
            <TextBlock Margin="3,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center" 
              Text="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/> 
           </StackPanel> 
          </Border> 
         </Viewbox> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

次にインスタンスです。

<Button Style="{StaticResource SSbutton}" 
     Tag="Some/Other/Image.png" 
     Width="90" Height="30" 
     Content="Desktop" 
     FontSize="13" Foreground="White"/> 

これがうまくいくことを願っています。

編集:OPのコメントどおり、WPFでTemplateBindingのためにパスの考慮事項を反映するために更新しました。

+0

試しましたか?私はそれを動作させることができませんでした。私はそれをコピー貼り付けましたが、イメージを表示しませんでした。私は何か間違っているのですか? –

+0

私は関連する質問をhttp://stackoverflow.com/questions/4638741/template-binding-in-control-templateで見つけ、 '{TemplateBinding Tag}'コードを{Binding Path = Tag、RelativeSource = {RelativeSource TemplatedParent}} 'それは良い作品です。この方法を使用して質問を更新したり、あなたの問題が解決したら、私はそれを受け入れます。 **注**:ただし、このメソッドは実行時にのみ機能します。画像は設計時ではなく、実行時にのみ表示されます。 –

+1

ああ申し訳ありませんが、私はxに注意を払わなかった:あなたのテンプレートをコピー/ペーストしたときにタイプし、質問にWPFタグがなかったので普遍的に勝ったと仮定した。イメージのビルドアクションをresourceに設定し、リテラルファイルパスの代わりにpack uriを使用すると、デザイン時にも機能するはずです。 –

関連する問題