依赖属性

WPF中的用户控件一般结合依赖属性使用,从而让用户在使用控件的时候可以给控件的属性自由赋值,下面就用一个简单的自定义按钮实例来说明其用法。

1、新建项目,选择"WPF用户控件库"

2、定义一个按钮类,新建三个依赖属性:ImageSourceNormalImageSourceMouseOverImageSourcePress,分别描述按钮在正常状态、鼠标经过、鼠标按下时显示的图片。

  1. namespace MyControl
  2. {
  3.     /// <summary>
  4.     /// 自定义按钮的分部类
  5.     /// </summary>
  6.     partial class UserButton : UserControl
  7.     {
  8.         #region 正常状态下显示的背景图片
  9.         public ImageSource ImageSourceNormal
  10.         {
  11.             get { return (ImageSource)GetValue(ImageSourceNormalProperty); }
  12.             set { SetValue(ImageSourceNormalProperty, value); }
  13.         }
  14.  
  15.         public static readonly DependencyProperty ImageSourceNormalProperty =
  16.             DependencyProperty.Register("ImageSourceNormal", typeof(ImageSource), typeof(UserButton),
  17.             new PropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/MyControl;component/Images/1.bmp"))));
  18.         #endregion
  19.  
  20.         #region 鼠标经过时显示的背景图片
  21.         public ImageSource ImageSourceMouseOver
  22.         {
  23.             get { return (ImageSource)GetValue(ImageSourceMouseOverProperty); }
  24.             set { SetValue(ImageSourceMouseOverProperty, value); }
  25.         }
  26.  
  27.         public static readonly DependencyProperty ImageSourceMouseOverProperty =
  28.             DependencyProperty.Register("ImageSourceMouseOver", typeof(ImageSource), typeof(UserButton),
  29.             new PropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/MyControl;component/Images/2.bmp"))));
  30.         #endregion
  31.  
  32.         #region 鼠标按下时显示的背景图片
  33.         public ImageSource ImageSourcePress
  34.         {
  35.             get { return (ImageSource)GetValue(ImageSourcePressProperty); }
  36.             set { SetValue(ImageSourcePressProperty, value); }
  37.         }
  38.  
  39.         public static readonly DependencyProperty ImageSourcePressProperty =
  40.             DependencyProperty.Register("ImageSourcePress", typeof(ImageSource), typeof(UserButton),
  41.             new PropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/MyControl;component/Images/3.bmp"))));
  42.         #endregion
  43.     }
  44. }

 

3、在xaml文件里定义按钮的模板样式

  1. <UserControl x:Class="MyControl.UserButton"
  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="23" Width="72">
  8.     <Grid>
  9.         <Button Width="72" Height="23">
  10.             <Button.Template>
  11.                 <ControlTemplate TargetType="Button">
  12.                     <Border x:Name="border" BorderBrush="Blue" BorderThickness="1" CornerRadius="3,3,3,3">
  13.                         <Border.Background>
  14.                             <ImageBrush ImageSource="{Binding ImageSourceNormal}"></ImageBrush>
  15.                         </Border.Background>
  16.                     </Border>
  17.                     <ControlTemplate.Triggers>
  18.                         <Trigger Property="IsMouseOver" Value="True">
  19.                             <Setter TargetName="border" Property="Background">
  20.                                 <Setter.Value>
  21.                                     <ImageBrush ImageSource="{Binding ImageSourceMouseOver}"></ImageBrush>
  22.                                 </Setter.Value>
  23.                             </Setter>
  24.                         </Trigger>
  25.                         <Trigger Property="IsPressed" Value="True">
  26.                             <Setter TargetName="border" Property="Background">
  27.                                 <Setter.Value>
  28.                                     <ImageBrush ImageSource="{Binding ImageSourcePress}"></ImageBrush>
  29.                                 </Setter.Value>
  30.                             </Setter>
  31.                         </Trigger>
  32.                     </ControlTemplate.Triggers>
  33.                 </ControlTemplate>
  34.             </Button.Template>
  35.         </Button>
  36.     </Grid>
  37. </UserControl>

 

4、在cs文件里定义上下文

  1. namespace MyControl
  2. {
  3.     /// <summary>
  4.     /// UserButton.xaml 的交互逻辑
  5.     /// </summary>
  6.     public partial class UserButton : UserControl
  7.     {
  8.         public UserButton()
  9.         {
  10.             InitializeComponent();
  11.  
  12.             this.DataContext = this;
  13.         }
  14.     }
  15. }


5、编译用户控件库,生成MyControl.dll

6、新建项目,选择"WPF 应用程序"

7、在xaml中引用MyControl命名空间,注意得在引用命名空间的字符串里加上程序集的名称

  1. <Window x:Class="WpfApplication1.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:mc="clr-namespace:MyControl;assembly=MyControl"
  5.         Title="MainWindow" Height="350" Width="525">
  6.     <Grid>
  7.         <mc:UserButton Margin="222,112,223,181"></mc:UserButton>
  8.         <mc:UserButton ImageSourceNormal=".\Images\10.bmp"
  9.                        ImageSourceMouseOver=".\Images\11.bmp"
  10.                        ImageSourcePress=".\Images\12.bmp"
  11.                        Margin="222,189,223,102"></mc:UserButton>
  12.     </Grid>
  13. </Window>


8、运行程序,就可以看到两个用户控件的效果,第一个显示的默认属性,第二个显示的是指定的属性。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值