自定义控件 因为没有办法对界面可视化编辑 所以用来很少
现在实现的是 自定义控件的 自定义属性 和自定义方法
用VS 创建自定义控件后 会自动创建 Themes 文件夹和 Generic.xaml 还有自定义的类 这边是SeachControl
Gneneric
<Style TargetType="{x:Type local:SeachControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SeachControl}">
<Grid>
<StackPanel Orientation="Horizontal" >
<TextBox Width="100" Height="20" Margin="0,0,5,0" Text="{Binding SearchText, RelativeSource={RelativeSource Mode=TemplatedParent},Mode=TwoWay}" Background="{TemplateBinding Background}"></TextBox>
<Button x:Name="button" Content="Select" Width="50" Height="20" ></Button>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
自定义控件类
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 WpfApplication24
{
/// <summary>
/// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
///
/// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:WpfApplication24"
///
///
/// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:WpfApplication24;assembly=WpfApplication24"
///
/// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
/// 并重新生成以避免编译错误:
///
/// 在解决方案资源管理器中右击目标项目,然后依次单击
/// “添加引用”->“项目”->[浏览查找并选择此项目]
///
///
/// 步骤 2)
/// 继续操作并在 XAML 文件中使用控件。
///
/// <MyNamespace:SeachControl/>
///
/// </summary>
public class SeachControl : Control
{
static SeachControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SeachControl), new FrameworkPropertyMetadata(typeof(SeachControl)));
}
public string SearchText
{
get { return (string)GetValue(SearchTextProperty); }
set { SetValue(SearchTextProperty, value); }
}
// Using a DependencyProperty as the backing store for SearchText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SearchTextProperty =
DependencyProperty.Register("SearchText", typeof(string), typeof(SeachControl), new PropertyMetadata(""));
public delegate void OnSeachClick(object ob, SearchEventArgs args);
public event OnSeachClick SeachButtenClick;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var button = GetTemplateChild("button");
if (button is Button)
{
(button as Button).Click += SeachControl_Click;
}
}
void SeachControl_Click(object sender, RoutedEventArgs e)
{
if (SeachButtenClick != null)
{
SeachButtenClick.Invoke(this, new SearchEventArgs() { SreachItem = SearchText });
}
}
}
}
创建参数类
public class SearchEventArgs:EventArgs
{
public string SreachItem { get; set; }
}
main 引用 这里可以看到自定义的事件
<Window x:Class="WpfApplication24.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyNamespace="clr-namespace:WpfApplication24"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<Grid>
<MyNamespace:SeachControl HorizontalAlignment="Center" SearchText="嗯嗯" VerticalAlignment="Center" Background="#FFCBCBCB" SeachButtenClick="SeachControl_SeachButtenClick" />
</Grid>
</Window>
Main的内容类
private void SeachControl_SeachButtenClick(object ob, SearchEventArgs args)
{
MessageBox.Show("HI "+ args.SreachItem);
}