ここでは、XAMLとコードでのボタンの形状を変えたい場合の差を見て、みなさんがどちらのコードでも作成できるようになるための参考になればなと思います。
やっていることは、ボタンの形状を二重丸に変えて、塗りつぶしています。
Window1.xaml
<window x:class="CustomButtonProject.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="Window1" height="300" width="300" loaded="Window_Loaded"> <grid> <button height="50" horizontalalignment="Left" margin="45,96,0,0" name="button1" verticalalignment="Top" width="50" click="button1_Click">Button</button> </grid> </window>
Window1.xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Collections; namespace CustomButtonProject { /// <summary> /// Window1.xaml の相互作用ロジック /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { ResourceDictionary rd = new ResourceDictionary(); foreach (DictionaryEntry t in this.Resources) { rd[t.Key] = t.Value; } ResourceDictionary theme = Application.LoadComponent(new Uri("CustomButton.xaml", UriKind.Relative)) as ResourceDictionary; rd.MergedDictionaries.Add(theme); this.Resources = rd; } private void button1_Click(object sender, RoutedEventArgs e) { Window2 w = new Window2(); w.ShowDialog(); } } }
CustomButton.xaml
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <style x:key="{x:Type Button}" targettype="{x:Type Button}"> <Setter Property="Template" > <Setter.Value > <ControlTemplate TargetType="{x:Type Button}" > <Grid Width="50" Height="50" > <Ellipse Fill="Black" StrokeThickness="2" Stroke="Aqua" / > <Ellipse Fill="Green" StrokeThickness="1" Stroke="Blue" Width="10" Height="10" / > </Grid > </ControlTemplate > </Setter.Value > </Setter > </style> </resourcedictionary>
Window2.xaml
<window x:class="CustomButtonProject.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="Window2" height="300" width="300"> <grid name="rootGrid" loaded="Grid_Loaded"> </grid> </window>
Window2.xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace CustomButtonProject { /// <summary> /// Window2.xaml の相互作用ロジック /// </summary> public partial class Window2 : Window { public Window2() { InitializeComponent(); } private void Grid_Loaded(object sender, RoutedEventArgs e) { ControlTemplate template = new ControlTemplate(typeof(Button)); FrameworkElementFactory elem1 = new FrameworkElementFactory(typeof(Grid)); FrameworkElementFactory ell1 = new FrameworkElementFactory(typeof(Ellipse)); ell1.SetValue(WidthProperty, Convert.ToDouble(50)); ell1.SetValue(HeightProperty, Convert.ToDouble(50)); ell1.SetValue(Path.FillProperty, Brushes.Red); ell1.SetValue(Path.StrokeThicknessProperty, Convert.ToDouble(2)); ell1.SetValue(Path.StrokeProperty, Brushes.Red); FrameworkElementFactory ell2 = new FrameworkElementFactory(typeof(Ellipse)); ell2.SetValue(WidthProperty, Convert.ToDouble(10)); ell2.SetValue(HeightProperty, Convert.ToDouble(10)); ell2.SetValue(Path.StrokeThicknessProperty, Convert.ToDouble(1)); ell2.SetValue(Path.StrokeProperty, Brushes.Black); ell2.SetValue(Path.FillProperty, Brushes.Plum); elem1.AppendChild(ell1); elem1.AppendChild(ell2); template.VisualTree = elem1; Button btn = new Button(); btn.Click += new RoutedEventHandler(button1_Click); btn.Template = template; this.rootGrid.Children.Add(btn); } private void button1_Click(object sender, RoutedEventArgs e) { MessageBox.Show("クリックされました。"); } } }