1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WPFでクリックすると画像の変わるボタン(XMLで描いたベクターイメージ)

Last updated at Posted at 2014-12-22

PNG画像を張り付けたものはサンプルがあったのですが、
XMLで描いたベクターイメージを張り付けるサンプルは見つからなかったので。。。
せっかくWPF使うならベクターで描きたいですよね。

以下のコードはクリックするとになるToggleButtonのサンプルです。

(ネストがひどいことになってるw)

MainWindow.xaml
<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ToggleButton Height="33" Width="33" Margin="68,64,0,0"
                      HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="Hand">
            <ToggleButton.Template>
                <ControlTemplate TargetType="ToggleButton">
                    <Canvas Name="ToggleButtonCanvas">
                        <Canvas.Background>
                            <VisualBrush>
                                <VisualBrush.Visual>
                                    <Canvas>
                                        <Ellipse StrokeThickness="2" Fill="#FFF4F4F5"  Stroke="Black"
                                                 Height="33" Width="33" Margin="5,5,5,5"
                                                 HorizontalAlignment="Left" VerticalAlignment="Top"/>
                                    </Canvas>
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Canvas.Background>
                    </Canvas>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter TargetName="ToggleButtonCanvas" Property="Background">
                                <Setter.Value>
                                    <VisualBrush>
                                        <VisualBrush.Visual>
                                            <Canvas>
                                                <Rectangle StrokeThickness="2" Fill="#FFF4F4F5" Stroke="Red"
                                                           Height="33" Width="33" Margin="0,0,0,0"
                                                           HorizontalAlignment="Left" VerticalAlignment="Top"/>
                                            </Canvas>
                                        </VisualBrush.Visual>
                                    </VisualBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </ToggleButton.Template>
        </ToggleButton>
    </Grid>
</Window>
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?