原文:WPF UserControl 的绑定事件、属性、附加属性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Vblegend_2013/article/details/83477473

 

WPF UserControl里可供绑定的属性

        /// <summary>
/// 重写基类 Margin
/// </summary>
public new Thickness Margin
{
get { return (Thickness)GetValue(MarginProperty); }
set { SetValue(MarginProperty, value); }
}
public new static readonly DependencyProperty MarginProperty = DependencyProperty.Register("Margin", typeof(Thickness), typeof(MirrorGrid), new FrameworkPropertyMetadata(new Thickness(0), new PropertyChangedCallback(OnMarginChanged)));
private static void OnMarginChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
((FrameworkElement)target).Margin = (Thickness)e.NewValue;
}

UserControl 里的事件路由

        /// <summary>
/// 声明路由事件
/// 参数:要注册的路由事件名称,路由事件的路由策略,事件处理程序的委托类型(可自定义),路由事件的所有者类类型
/// </summary>
public static readonly RoutedEvent OnToolTipShowEvent = EventManager.RegisterRoutedEvent("OnToolTipShow", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(UserControl));
/// <summary>
/// 处理各种路由事件的方法
/// </summary>
public event RoutedEventHandler OnToolTipShow
{
//将路由事件添加路由事件处理程序
add { AddHandler(OnToolTipShowEvent, value,false); }
//从路由事件处理程序中移除路由事件
remove { RemoveHandler(OnToolTipShowEvent, value); }
}

路由事件


private void RoutedToolTipShow(RoutedEventArgs param)
{
param.RoutedEvent = OnToolTipShowEvent;
param.Source = this;
this.RaiseEvent(param);
}

控件附加属性

  public abstract class AquariumServices : DependencyObject
{
public enum Bouyancy {Floats,Sinks,Drifts} public static readonly DependencyProperty BouyancyProperty = DependencyProperty.RegisterAttached(
"Bouyancy",
typeof(Bouyancy),
typeof(AquariumServices),
new PropertyMetadata(Bouyancy.Floats)
);
public static void SetBouyancy(DependencyObject element, Bouyancy value)
{
element.SetValue(BouyancyProperty, value);
}
public static Bouyancy GetBouyancy(DependencyObject element)
{
return (Bouyancy)element.GetValue(BouyancyProperty);
}
}

 

WPF UserControl 的绑定事件、属性、附加属性的更多相关文章

  1. WPF的DataTrigger绑定自身属性

    原文:WPF的DataTrigger绑定自身属性 <DataTrigger Binding="{Binding RelativeSource={RelativeSource self} ...

  2. [WPF]UserControl的MouseWheel事件触发

    用户控件: <UserControl> <Grid> <TextBox x:Name="textBlock" HorizontalAlignment= ...

  3. WPF MultiBinding后台绑定动态属性 属性改变不调用Convert的问题

    一开始的写法: MultiBinding mb = new MultiBinding(); Binding b1 = new Binding(); b1.ElementName = "tex ...

  4. WPF UserControl响应窗体的PreviewKeyDown事件

    目的 在UserControl页面实现通过快捷键打开新建窗口 实现过程 监听Window窗体的PreviewKeyDown 其实,使用KeyDown事件也是可以的 页面代码 <Window x: ...

  5. WPF ----在UserControl的xaml里绑定依赖属性

    场景:在定义wpf 用户控件的时候,希望使用时设置自定义的属性来改变用户控件里的状态或内容等. 下面直接上实例代码: 用户控件的后台代码,定义依赖属性 public partial class MyU ...

  6. 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试

    原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...

  7. 整理:WPF中应用附加事件制作可以绑定命令的其他事件

    原文:整理:WPF中应用附加事件制作可以绑定命令的其他事件 目的:应用附加事件的方式定义可以绑定的事件,如MouseLeftButton.MouseDouble等等 一.定义属于Control的附加事 ...

  8. jQuery1.9为动态添加元素绑定事件以及获取和操作checkbox的选择属性

    1.jQuery为动态添加的元素绑定事件:在1.7之后,添加了live()方法,1.9后又被移除,1.9中可用on()方法: $(function() { $('.btn').on('click',  ...

  9. jQuery动态添加li标签并添加属性和绑定事件

    代码如下: <%@page import="java.util.ArrayList"%> <%@ page language="java" c ...

随机推荐

  1. 【习题5-5 UVA-10391】Compound Words

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举每一个串的分割点. 看看左右两个串在不在字符串中即可. [代码] #include <bits/stdc++.h> ...

  2. 【习题5-3 UVA-10935】Throwing cards away I

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用STL的queue写 [代码] #include <bits/stdc++.h> using namespace st ...

  3. 【47.76%】【Round #380B】Spotlights

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. jQuery常用方法(持续更新) jQuery(转)

    0.常用代码: 请容许我在1之前插入一个0,我觉得我有必要把最常用的代码放在第一位,毕竟大部分时间大家都是找代码的. (1)AJAX请求 $(function() { $('#send').click ...

  5. proxool数据库连接池用法

    今天给大家介绍一种新的数据连接池实现方式--proxool数据库连接池,这是一个健壮.易用的连接池.以下通过一个Demo说明一下怎样使用: 项目结构例如以下: DBLink.java文件里的代码: p ...

  6. 点滴记录——学习Redis笔记

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/39701409 Redis 默认port6379 Redis适用场景 1. 取最新N个数据的操 ...

  7. Android内存优化杂谈

    Android内存优化是我们性能优化工作中比较重要的一环,这里其实主要包括两方面的工作: 优化RAM,即降低运行时内存.这里的目的是防止程序发生OOM异常,以及降低程序由于内存过大被LMK机制杀死的概 ...

  8. 【例题5-1 UVA 10474 】Where is the Marble?

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 排序 用lower_bound找就可以了. ->lower_bound,如果里面所有的数字都比x小,那么它的返回值会越界! [ ...

  9. 【22.95%】【hdu 5992】Finding Hotels

    Problem Description There are N hotels all over the world. Each hotel has a location and a price. M ...

  10. Giraph源代码分析(六)——Edge 分析

    HamaWhite 原创,转载请注明出处.欢迎大家增加Giraph 技术交流群: 228591158 欢迎訪问: 西北工业大学 - 大数据与知识管理研究室 (Northwestern Polytech ...