正常定义的以来属性,在XAML里设置值得时候是不触发Setter的,只能在code中用。
监听PropertyChangedCallback事件可以感知XAML里的设置,这样才能code和XAML配合使用。
http://stackoverflow.com/questions/5644081/wpf-usercontrol-dependency-property-setter-not-triggering
<UserControl x:Class="WpfApplication1.UserControl1"
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"
d:DesignHeight="300" d:DesignWidth="300" Background="Black">
<Grid>
<Button Name="b" Content="Button" Margin="27,26,0,0" Click="b_Click" Height="22" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75"/> </Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApplication1
{
/// <summary>
/// UserControl1.xaml の相互作用ロジック
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
} public static readonly DependencyProperty IsAAAProperty =
DependencyProperty.Register(
"IsAAA", typeof(Boolean),
typeof(UserControl1),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnUriChanged) )
); private static void OnUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue == true)
{
UserControl1 u = (UserControl1)d;
u.b.Background = Brushes.Black;
}
MessageBox.Show("event");
} public bool IsAAA
{
get {
MessageBox.Show("get");
return (bool)GetValue(IsAAAProperty);
}
set {
MessageBox.Show("set");
SetValue(IsAAAProperty, value);
}
} private void b_Click(object sender, RoutedEventArgs e)
{ }
}
}

http://www.cjavaphp.com/q/answers-dependency-property-not-working-trying-to-set-through-style-setter-1593523.html

The correct way of implementing a source for an Image in a user control in my opinion is not BitmapSouce. The easiest and best way (according to me again) is using Uri.

Change your dependency property to this (while also defining a change callback event):

ImageSourceProperty = DependencyProperty.Register(
"ImageSource", typeof (Uri), typeof (MyButton),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnImageSourceChanged)));

and the property to this:

public Uri ImageSource
{
get
{
return (Uri)GetValue(ImageSourceProperty);
}
set
{
SetValue(ImageSourceProperty, value);
}
}

Where your call back is like this:

private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
MyButton hsb = (MyButton)sender; Image image = hsb.tehImage;
image.Source = new BitmapImage((Uri) e.NewValue);
}

WPF自定义组件,自定义属性(依赖属性)的更多相关文章

  1. WPF学习笔记一 依赖属性及其数据绑定

    本文想通过由浅入深的讲解让读者比较深的理解依赖属性.  首先,我们回顾一下依赖属性的发展历史. 最初,人们提出面向对象编程时,并没有属性这个说法,当时叫做成员变量.一个对象由成员变量和成员函数组成,如 ...

  2. WPF学习笔记二 依赖属性实现原理及性能分析

    在这里讨论依赖属性实现原理,目的只是学习WPF是怎么设计依赖属性的,同时更好的使用依赖属性. 首先我们来思考一个简单的问题:我们希望能验证属性的值是否有效,属性变更时进行自己的处理.回顾一下.net的 ...

  3. WPF xaml中列表依赖属性的定义

    列表内容属性 如上图,是一个列表标题排序控件,我们需要定义一个标题列表,从而让调用方可以自由的设置标题信息. 在自定义控件时,会遇到列表依赖属性,那么该如何定义呢? 下面是错误的定义方式: /// & ...

  4. WPF自学入门(五)WPF依赖属性

    在.NET中有事件也有属性,WPF中加入了路由事件,也加入了依赖属性.最近在写项目时还不知道WPF依赖属性是干什么用的,在使用依赖项属性的时候我都以为是在用.NET中的属性,但是确实上不是的,通过阅读 ...

  5. WPF依赖属性

    原文:http://www.cnblogs.com/xiongpq/archive/2010/06/29/1767905.html 概述: Windows Presentation Foundatio ...

  6. WPF快速入门系列(2)——深入解析依赖属性

    一.引言 感觉最近都颓废了,好久没有学习写博文了,出于负罪感,今天强烈逼迫自己开始更新WPF系列.尽管最近看到一篇WPF技术是否老矣的文章,但是还是不能阻止我系统学习WPF.今天继续分享WPF中一个最 ...

  7. WPF基础到企业应用系列7——深入剖析依赖属性(WPF/Silverlight核心)

    一. 摘要 首先圣殿骑士非常高兴这个系列能得到大家的关注和支持.这个系列从七月份開始到如今才第七篇,上一篇公布是在8月2日,掐指一算有二十多天没有继续更新了,最主要原因一来是想把它写好,二来是由于近期 ...

  8. 【WPF学习笔记】之依赖属性

    概述: Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可用于扩展公共语言运行时 (CLR) 属性的功能.这些服务通常统称为 WPF 属性系统.由 ...

  9. WPF依赖属性的正确学习方法

    前言 我在学习WPF的早期,对依赖属性理解一直都非常的不到位,其恶果就是,我每次在写依赖属性的时候,需要翻过去的代码来复制黏贴. 相信很多朋友有着和我相同的经历,所以这篇文章希望能帮助到那些刚刚开始学 ...

随机推荐

  1. MooseFS技术详解

    原文 http://www.tuicool.com/articles/vQvEZ3y MooseFS是一款具有冗余容错功能的分布式文件系统.它把数据分散在多台服务器上,确保一份数据多个备份副本,对外提 ...

  2. IIS优化整理

    IIS 之 在IIS7.IIS7.5中应用程序池最优配置方案 找到Web站点对应的应用程序池,“应用程序池” → 找到对应的“应用程序池” → 右键“高级设置...” 一.一般优化方案 1.基本设置 ...

  3. Mysql5.7基于事务转为基于日志

    先决条件 master端执行 flush logs;show master status; 获取file和position slave端执行 stop slave; change master to ...

  4. mui 禁止透明背景点击关闭弹窗遮罩

    //禁止关闭遮罩 window.addEventListener('tap', function(e) { e.target.className == 'mui-backdrop mui-active ...

  5. android 命令行签名apk文件

    签名apk 1.将apk格式改为zip格式包,然后删除原来apk里面的META-INF文件夹,之后改回apk文件格式 2.cmd命令行: jarsigner -verbose -keystore C: ...

  6. JS判断键盘上的上下左右键

    document.onkeydown=function(event){ var e = event || window.event || arguments.callee.caller.argumen ...

  7. CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)

    题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...

  8. 浅谈js数据类型识别方法

    js有6种基本数据类型  Undefined , Null , Boolean , Number , String ,Symbol和一种引用类型Object,下面我们就来一一看穿,哦不,识别他们. t ...

  9. 【hive】where使用注意的问题

    不能再where后边使用别名,group by后边也一样不能使用别名 select id,col1 - col2 from table1 where (col1 - col2) > 1000;  ...

  10. poj2771

    题解: 二分图最大独立及 每两个不能选的渐变 输出n+m-最大匹配 代码: #include<cstdio> #include<cmath> #include<algor ...