1
私はカスタムボタンStyle
をXAML
で書いています。画像とテキストのボタンです。 しかし、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>
私はそれをどのように行うことができますか?
試しましたか?私はそれを動作させることができませんでした。私はそれをコピー貼り付けましたが、イメージを表示しませんでした。私は何か間違っているのですか? –
私は関連する質問をhttp://stackoverflow.com/questions/4638741/template-binding-in-control-templateで見つけ、 '{TemplateBinding Tag}'コードを{Binding Path = Tag、RelativeSource = {RelativeSource TemplatedParent}} 'それは良い作品です。この方法を使用して質問を更新したり、あなたの問題が解決したら、私はそれを受け入れます。 **注**:ただし、このメソッドは実行時にのみ機能します。画像は設計時ではなく、実行時にのみ表示されます。 –
ああ申し訳ありませんが、私はxに注意を払わなかった:あなたのテンプレートをコピー/ペーストしたときにタイプし、質問にWPFタグがなかったので普遍的に勝ったと仮定した。イメージのビルドアクションをresourceに設定し、リテラルファイルパスの代わりにpack uriを使用すると、デザイン時にも機能するはずです。 –