以实例讲解(大部分讲解在代码中)


1,新建一个WPF项目,添加一个用户控件之后在用户控件里面添加几个控件用作测试,

  1. <UserControl x:Class="SelfControlDenpendy.SelfControl"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="300" d:DesignWidth="300">
  8. <Grid>
  9. <StackPanel Width="200" Height="250" Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
  10. <TextBox Width="180" Height="50" x:Name="id" Margin="5"></TextBox>
  11. <TextBox Width="180" Height="50" Text="{Binding ElementName=id,Path=Text}" Margin="5"></TextBox>
  12. <Button Height="26" Width="120" x:Name="show" Margin="5" Content="测试绑定控件自身命令"
  13. Command="{Binding ChangedIndexCommand}"
  14. CommandParameter="{Binding ElementName=id,Path=Text}"></Button>
  15. <Button Height="26" Width="120" x:Name="buttonout" Margin="5"
  16. Command="{Binding ButtonCommend, Mode=TwoWay}" Content="测试绑定外部命令" ></Button>
  17. </StackPanel>
  18. </Grid>
  19. </UserControl>

  2,打开用户控件后台代码编写依赖属性以及命令(需要引入GalaSoft.MvvmLight.dll)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14.  
  15. namespace SelfControlDenpendy
  16. {
  17. /// <summary>
  18. /// SelfControl.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class SelfControl : UserControl
  21. {
  22. public SelfControl()
  23. {
  24. InitializeComponent();
  25. DataContext = this;//注意此处用于绑定的源必须写,也可把所有依赖代码写在ViewModel里面
  26. ChangedIndexCommand= new RelayCommand<string>(AutoPaging);//注意带参数的命令的写法
  27. }
  28. void AutoPaging(string s)//命令执行带参数的方法
  29. {
  30. MessageBox.Show(s);
  31. }
  32. static SelfControl()//依赖属性的赋值必须写在静态函数里面因为是依赖属性静态只读在家上此处需要在初始化时就注册依赖属性古写在此处
  33. { //也可不写在此处而写在字段定义出对其直接赋值
  34. UIPropertyMetadata md = new UIPropertyMetadata("", PropertyInputNameChanged);//第一个参数代表默认值(注意类型)
  35. SelfControl.InputNameProperty = DependencyProperty.Register("InputName", typeof(string), typeof(SelfControl), md);//最后一个参数用于当依赖属性值改变是调用md指定的方法
  36. ChangedCommandProperty =//内部访问命令
  37. DependencyProperty.Register("ChangedCommand", typeof(ICommand), typeof(SelfControl), new PropertyMetadata(null));//不许要默认值时写null或不要最后一个参数
  38. ButtonCommendProperty =//外部绑定命令
  39. DependencyProperty.Register("ButtonCommend", typeof(ICommand), typeof(SelfControl), new PropertyMetadata(null));
  40. }
  41.  
  42. public ICommand ButtonCommend
  43. {
  44. get { return (ICommand)GetValue(ButtonCommendProperty); }
  45. set { SetValue(ButtonCommendProperty, value); }
  46. }
  47. public ICommand ChangedCommand
  48. {
  49. get { return (ICommand)GetValue(ChangedCommandProperty); }
  50. set { SetValue(ChangedCommandProperty, value); }
  51. }
  52. public static readonly DependencyProperty ChangedCommandProperty;//可以不用敲代码只用输入propdp按Tab即可完成编写
  53. public static readonly DependencyProperty InputNameProperty;
  54. public static readonly DependencyProperty ButtonCommendProperty;
  55. public string InputName
  56. {
  57. get { return (string)GetValue(SelfControl.InputNameProperty); }
  58. set { SetValue(SelfControl.InputNameProperty, value); }
  59. }
  60.  
  61. private static void PropertyInputNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)//此方法非常重要可用于编写逻辑代码
  62. {
  63.  
  64. SelfControl sc = (SelfControl)d;
  65.  
  66. string str = (string)e.NewValue;//外部赋给依赖属性的值(注意依赖属性的类型)
  67.  
  68. if (str != "" && str != null)
  69. {
  70. sc.id.Text = str.ToString();
  71. }
  72. }
  73. }
  74. }
  75. }

  3,在主窗口中就可以使用此用户控件(注意引入命名空间)

  1. <Window x:Class="Dependency.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:loac="clr-namespace:Dependency"
  5. Title="MainWindow" Height="350" Width="600">
  6. <Grid>
  7. <TextBox x:Name="tt" Margin="428,70,32,208"></TextBox>
  8. <loac:SelfControl x:Name="sc" Margin="10,10,189,60" RenderTransformOrigin="0.456,0.7"
  9. ButtonCommend="{Binding AutoCommand}"></loac:SelfControl>
  10. <Button x:Name="bt" Margin="225,283,49,0" Content="测试依赖属性" Click="bt_Click"></Button>
  11. </Grid>
  12. </Window>

  4,后台逻辑代码

  1. using GalaSoft.MvvmLight.Command;
  2. using System.Windows;
  3. using System.Windows.Input;
  4.  
  5. namespace Dependency
  6. {
  7. public partial class MainWindow : Window
  8. {
  9.  
  10. public MainWindow()
  11. {
  12. InitializeComponent();
  13. DataContext = this;
  14. AutoCommand = new RelayCommand(showcommend);//不带参数的命令
  15. AddUserCommand = new RelayCommand(ExecuteAddUser, CanExecuteAddUser);//添加判断的命令
  16. bt.Command = AutoCommand;//赋值命令,用于关联命令
  17. sc.ButtonCommend = AutoCommand;//关联依赖命令
  18. }
  19. void showcommend()
  20. {
  21. MessageBox.Show("测试");
  22. }
  23. public ICommand AutoCommand { get; private set; }
  24. private ICommand m_ButtonCommand;//此处并未使用此命令,只是显示一般命令的写法
  25. public ICommand ButtonCommand
  26. {
  27. get
  28. {
  29. return m_ButtonCommand;
  30. }
  31. set
  32. {
  33. m_ButtonCommand = value;
  34. }
  35. }
  36. public ICommand AddUserCommand { get; private set; }
  37. void ExecuteAddUser()
  38. {
  39. //逻辑代码
  40. }
  41. bool CanExecuteAddUser()
  42. {
  43. //逻辑代码
  44. return true;
  45. }
  46. private void bt_Click(object sender, RoutedEventArgs e)
  47. {
  48. sc.InputName = tt.Text.ToString();
  49. AutoCommand.Execute(null);//执行定义的命令可代替bt.Command = AutoCommand;
  50. }
  51. }
  52. }

  1,若出现在XAML使用用户控件依赖命令时出现“未识别或无法访问”的提示时重新编译或生成一下即可

2,有可能出现把自定义命令赋值给用户控件的命令但是出现没有调用执行方法,此时建议改用绑定关联,或检查用户控件中使用命令的位置是否合适

3,依赖属性的默认值类型错误,注意修改依赖属性注册处的最后一个参数

WPF利用依赖属性和命令编写自定义控件的更多相关文章

  1. WPF 使用依赖属性自定义控件

    使用依赖属性自定义控件,依赖属性必须定义在自定义控件中,不能定义在其他文件中 一.先实现一个类继承你要复写的类 using System; using System.Collections.Gener ...

  2. WPF的依赖属性

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

  3. 利用windows系统ftp命令编写的BAT文件上传[转]

    利用windows系统ftp命令编写的BAT文件上传[转] 利用windows系统ftp命令编写的BAT文件上传[转] 在开发中往往需要将本地的程序上传到服务器,而且用惯了linux命令的人来说.在w ...

  4. wpf 的依赖属性只能在loaded 事件之后才能取到

    wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的  InitializeComponent(); 之后取不到 wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的  ...

  5. WPF 中依赖属性的继承(Inherits)

    WPF中依赖属性的值是是可以设置为可继承(Inherits)的,这种模式下,父节点的依赖属性会将其值传递给子节点.例如,数据绑定中经常使用的DataContextProperty: var host ...

  6. WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性

    原文:WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性 如果你要自定义一个图片按钮控件,那么如何在主窗体绑定这个控件上图片的Source呢? ...

  7. WPF的依赖属性和附加属性(用法解释较全)

    转:https://www.cnblogs.com/zhili/p/WPFDependencyProperty.html 一.引言 感觉最近都颓废了,好久没有学习写博文了,出于负罪感,今天强烈逼迫自己 ...

  8. WPF: 只读依赖属性的介绍与实践

    在设计与开发 WPF 自定义控件时,我们常常为会控件添加一些依赖属性以便于绑定或动画等.事实上,除了能够添加正常的依赖属性外,我们还可以为控件添加只读依赖属性(以下统称"只读属性" ...

  9. 说说WPF的依赖属性

    首先,我们先来大概了解一下依赖属性 什么是依赖属性:依赖属性自己没有值,通过依赖别人(如Binding)来获得值. 依赖属性为什么会出现:控件常用字段有限,包装太多属性会占用过高内存,造成浪费.所以用 ...

随机推荐

  1. 04737_C++程序设计_第4章_类和对象

    例4.1 描述点的Point类. 例4.2 根据上面对Point类的定义,演示使用Point类的对象. #define _SCL_SECURE_NO_WARNINGS #include <ios ...

  2. CSS实现倒影-------Day80

    发现这个功能的时候非常开心,结果不想居然是个残次品,让我不禁想起了"天龙八部"上段誉的六脉神剑,在这个浏览器上能够.在还有一个上就无论了啊,时灵时不灵的,只是有总比没有要来的好,一 ...

  3. 自学SQL语言的例子(使用MySQL实现)

    SQL语言作为一种数据库管理的标准语言有着极为广泛的应用场景,菜鸟入门选用的数据库软件是轻量级的免费(这个极为重要)的MySQL,下载链接如下:http://www.mysql.com/downloa ...

  4. SQL Server执行计划那些事儿(1)——哈希、合并、嵌套联接的选择

    接下来的文章是记录自己曾经的盲点,同时也透漏了自己的发展历程(可能发展也算不上,只能说是瞎混).当然,一些盲点也在工作和探究过程中慢慢有些眉目,现在也愿意发扬博客园的奉献精神,拿出来和大家分享一下. ...

  5. [转]不用Cookie的“Cookie”技术

    有另外一种比较隐蔽的用户追踪技术,不使用cookie或者Javascript.很多网站已经在用了,但知道的人不多.本文就来介绍一下这种技术是如何追踪用户,用户又该如何避免追踪. 这种技术不依赖于: C ...

  6. eclipse - tomcat 远程调试

    步骤:前提是tomcat上应用是eclipse打包部署上去的,代码一致. 1,在机器A上部署应用remote-debug之前,需要为机器A上的tomcat配置调试端口.在${tomcat}/bin下加 ...

  7. lightoj 1030

    递推,倒着递推. #include<stdio.h> #define maxn 1010 #define min(a,b) (a)>(b)?(b):(a) int main() { ...

  8. php7 install memcache extension

    #download source code package from git $ git clone https://github.com/websupport-sk/pecl-memcache.gi ...

  9. zabbix监控服务器部署

    1.服务器安装lamp环境 [root@monitor ~]# yum  install gcc gcc-c++ autoconf httpd php mysql mysql-server php-m ...

  10. Unix/Linux环境C编程入门教程(9) unbntu CCPP开发环境搭建

    1.      首先启动VMware,如果没有安装,请查看前面VMware的安装视频 2 启动虚拟机向导,选择自定义 3 单击下一步 4 选择稍后安装操作系统 5 .选择unbntu 64linux ...