MVKbgl, Thanks for your Question. I have Updated My Answer.
To make a button or any other control you might need to inherit the Control and then could add your custom property. Here I have created a custom button named EclipseButton which is usually a usercontrol. Please have a look on the XAMl on the control
<UserControl x:Class="WpfTest.EclipseButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
>
<Grid>
<Button>
<StackPanel Orientation="Horizontal" Margin="10">
<Ellipse Name="ellipse1" Stroke="Black"
Width="{Binding ElementName=UC, Path=ButtonWidth}"
Height="{Binding ElementName=UC, Path=ButtonHeight}"
/>
<TextBlock Text="{Binding ElementName=UC, Path=Text}"
Margin="10,0,0,0"/>
</StackPanel>
</Button>
</Grid>
</UserControl>
And the code behind of this control looks like that,
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;
namespace WpfTest
{
public partial class EclipseButton : UserControl
{
public EclipseButton()
{
InitializeComponent();
}
public double ButtonWidth
{
get { return (double)GetValue(WidthProperty); }
set { SetValue(WidthProperty, value); }
}
public static readonly DependencyProperty WidthProperty =
DependencyProperty.Register("ImageWidth", typeof(double), typeof(EclipseButton), new UIPropertyMetadata(16d));
public double ButtonHeight
{
get { return (double)GetValue(HeightProperty); }
set { SetValue(HeightProperty, value); }
}
public static readonly DependencyProperty HeightProperty =
DependencyProperty.Register("ImageHeight", typeof(double), typeof(EclipseButton), new UIPropertyMetadata(16d));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(EclipseButton), new UIPropertyMetadata(""));
}
}
And now the XAML of main window is ,
<Window x:Class="WpfTest.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"
xmlns:my="clr-namespace:WpfTest">
<Grid Height="311" Width="474">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="188,263,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />
<Label Content="Hi!!" Height="100" HorizontalAlignment="Center" Margin="188,76,186,0" Name="label1" VerticalAlignment="Top" Width="100" Background="#FFBBF23E" />
<my:EclipseButton Text="Eclipse Button" HorizontalAlignment="Left" VerticalAlignment="Top"
ButtonWidth="16" ButtonHeight="16" Margin="10" ButtonBase.Click="Button_Click" />
</Grid>
</Window>
And the Code behind of this main window is
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;
namespace WpfTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
Grid grd = btn.Parent as Grid;
Label lbl = grd.Children[1] as Label;
if (lbl.Width < 100)
{
lbl.Width = 100;
}
else
{
lbl.Width = 50;
}
if (lbl.Height < 100)
{
lbl.Height = 100;
}
else
{
lbl.Height = 50;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
EclipseButton btn = (EclipseButton)sender;
Grid grd = btn.Parent as Grid;
Label lbl = grd.Children[1] as Label;
if (lbl.Width < 100)
{
lbl.Width = 100;
}
else
{
lbl.Width = 50;
}
if (lbl.Height < 100)
{
lbl.Height = 100;
}
else
{
lbl.Height = 50;
}
}
}
}
In this code you will see that i have used my own button which has a Click Event and this event is handle in the code behind of main window.
I think it will help you enough. And let me know if you need any assistance please.
Thanks,
Rashim