|
Hi! Began studying wpf. Versed with custom control'ami. I create the control (simple button) from Control.
The question is - is it possible to not draw method OnRend (such as they call it), and make the template, the file generic.xaml? And still do not understand why does not my click handler.
файл MyButton.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.Globalization;
namespace My_Lib
{
public class MyButton : Control
{
public static readonly RoutedEvent MyClickEvent;
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
MyClickEvent = EventManager.RegisterRoutedEvent("MyClick", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(MyButton));
}
public event RoutedEventHandler MyClick
{
add {
base.AddHandler(MyButton.MyClickEvent, value);
}
remove {
base.RemoveHandler(MyButton.MyClickEvent, value);
}
}
}
}
файл generic.xaml
xmlns:local="clr-namespace:My_Lib">
<LinearGradientBrush x:Key="Gray"
StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="PaleTurquoise" Offset="0" />
<GradientStop Color="#CCCCFF" Offset="0.5" />
<GradientStop Color="Teal" Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="Gray2"
StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="PaleGreen" Offset="0" />
<GradientStop Color="#99CC99" Offset="0.5" />
<GradientStop Color="Green" Offset="1" />
</LinearGradientBrush>
<Style TargetType="{x:Type local:MyButton}">
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="40"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyButton}">
<Border
x:Name="ButtonBorder"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
BorderBrush="White"
CornerRadius="25"
BorderThickness="2"
Background="{StaticResource Gray}"
Opacity="0.9"
>
<ContentPresenter x:Name="ButtonContentPresenter"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"
>
</ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonBorder"
Property="Background"
Value="{StaticResource Gray2}">
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
файл MainWindow.xaml.cs
namespace MyComponentsTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void myButton1_MyClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("sd");
}
}
}
|
|
|
|