WPF 精修篇 自定义控件
自定义控件 因为没有办法对界面可视化编辑 所以用来很少
现在实现的是 自定义控件的 自定义属性 和自定义方法

用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);
-
}
WPF 精修篇 自定义控件的更多相关文章
- WPF 精修篇 数据触发器
原文:WPF 精修篇 数据触发器 数据触发器 可以使用Binding 来绑定控件 或者数据源 来触发相关动作 举栗子 <Window.Resources> <Style Target ...
- WPF 精修篇 属性触发器
原文:WPF 精修篇 属性触发器 属性触发器是通过 某个条件触发改变属性 通过无代码实现功能 <Style TargetType="{x:Type Label}"> ...
- WPF 精修篇 样式继承
原文:WPF 精修篇 样式继承 这个 是新知识 样式可以继承 <Style x:Key="TextBlockStyleBase" TargetType="{x:Ty ...
- WPF 精修篇 用户控件
原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs ...
- WPF 精修篇 DataGrid 筛选
原文:WPF 精修篇 DataGrid 筛选 DataGrid也可以分组 但是用的地方不多 就没写 筛选还是可以的 比如Datagrid数据量比较大 要做数据筛选 贴码 <DataGrid x: ...
- WPF 精修篇 DataGrid 数据源排序
原文:WPF 精修篇 DataGrid 数据源排序 效果 <DataGrid x:Name="datagrid" ItemsSource="{Binding Ele ...
- WPF 精修篇 数据绑定到对象
原文:WPF 精修篇 数据绑定到对象 数据绑定到对象 首先 我们需要一个对象 public class Preson { private string name; public string Name ...
- WPF 精修篇 数据绑定 更新通知
原文:WPF 精修篇 数据绑定 更新通知 开始更新一点有意思的了 首先 数据绑定 其中之一 Element 绑定 看例子 <Window x:Class="WpfApplicatio ...
- WPF 精修篇 依赖属性
原文:WPF 精修篇 依赖属性 依赖属性使用场景 1. 希望可在样式中设置属性. 2. 希望属性支持数据绑定. 3. 希望可使用动态资源引用设置属性. 4. 希望从元素树中的父元素自动继承属性值. 5 ...
随机推荐
- Attach Files to Objects 将文件附加到对象
In this lesson, you will learn how to attach file collections to business objects. For this purpose, ...
- Java_可变参数类型
Java方法中的可变参数类型,也称为不定参数类型,是一个非常重要的概念 举栗子 public class TestVarArgus { public static void dealArray(int ...
- 使用Eclipse开发Web项目(JSP)——简单登录、无sql
1.使用Eclipse开发Web项目(JSP) tomcat 2.在Eclipse中创建的Web项目: 浏览器可以直接访问webContent中的文件 例如http://localhost:8080/ ...
- getattribute方法,Python属性访问拦截器的用法
__getattribute__()方法是属性访问时的拦截器,每当访问属性的时候,会先执行这个方法,然后再执行访问属性的操作步骤,可以用来记录属性访问的log.代码示例如下: class Itca ...
- canvas绘制线条详解
canvas详解----绘制线条 <!DOCTYPE html> <html> <head> <title>canvas详解</title> ...
- 【Postman】举例实战——天气查询
准备工作: 1.下载postman(搜索官网下载即可) 2.接口文档(以聚合上去免费API:天气查询接口) 打开postman: 1.输入url 2.请求方式 3.请求参数 4.在postman中运行 ...
- ubuntu18.04因java路径原因启动jenkins失败
我的云服务器ubuntu18.04上本来装了jenkins,今天安装完tomcat后,将原有的openjdk卸载了,安装了jdk8u192, 此时浏览器访问8080端口显示的就是tomcat安装成功的 ...
- PHP 管理树莓派
同学给过我一块树莓派,那会儿觉得挺新鲜的.但是每次使用都需要远程桌面或者 ssh 进行登录,比较麻烦.后来为了方便管理,在树莓派上安装部署了 LAMP 环境,然后写了一个简单的 PHP 页面,代码如下 ...
- SpringCloud的入门学习之概念理解、Config配置中心
1.SpringCloud Config分布式配置中心.分布式系统面临的配置问题. 答:微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个 ...
- C#属性方法 构造函数(不知道自己理解的对不对)
using System; namespace test { class Program { static void Main(string[] args) { Cat kitty = new Cat ...