WPF Blend 自定义控件
#1.使用Blend 设计控件
<UserControl x:Class="WpfApp25.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" xmlns:local="clr-namespace:WpfApp25"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><UserControl.Resources><local:StatusColorConverter x:Key="statusColorConverter"></local:StatusColorConverter></UserControl.Resources><Grid><Ellipse HorizontalAlignment="Left" Height="100" Stroke="#FFF5F2F2" VerticalAlignment="Top" Width="100" StrokeThickness="5"Fill="{Binding StatusP, Converter={StaticResource statusColorConverter},UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UserControl1}}}"></Ellipse></Grid>
</UserControl>
#2.给控件添加自定义属性
using System;
using System.Collections.Generic;
using System.Globalization;
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 WpfApp25
{public enum Status { Normal, Good, Bad }public class StatusColorConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){if (value is Status && value != null){Status status = (Status)value;var color = Colors.Gray;switch (status){case Status.Good:color = Colors.Green; break;case Status.Bad:color = Colors.Red; break;}return new LinearGradientBrush(color,Colors.White,100.0);}return null;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotImplementedException();}}public partial class UserControl1 : UserControl{public Status StatusP{get { return (Status)GetValue(StatusPProperty); }set { SetValue(StatusPProperty, value); }}public static readonly DependencyProperty StatusPProperty =DependencyProperty.Register("StatusP", typeof(Status), typeof(UserControl1), new PropertyMetadata(Status.Normal));public UserControl1(){InitializeComponent();StatusP = Status.Bad;}}
}
#3.MainWindow添加调用
<Window x:Class="WpfApp25.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp25"mc:Ignorable="d"Title="MainWindow" Height="522" Width="814"><Grid x:Name="xgrid" Background="#FFDCD3D3"><local:UserControl1 x:Name="xbtn" Margin="110,190,577,129" ></local:UserControl1><Button Click="Button_Click" Content="Normal" HorizontalAlignment="Left" Height="73.443" Margin="341.846,150.596,0,0" VerticalAlignment="Top" Width="233.679"/><Button Click="Button_Click1" Content="Green" HorizontalAlignment="Left" Height="73" Margin="342,365,0,0" VerticalAlignment="Top" Width="234"/><Button Click="Button_Click2" Content="Red" HorizontalAlignment="Left" Height="73" Margin="342,255,0,0" VerticalAlignment="Top" Width="234"/></Grid>
</Window>
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 WpfApp25
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void Button_Click(object sender, RoutedEventArgs e){xbtn.StatusP = Status.Normal;}private void Button_Click1(object sender, RoutedEventArgs e){xbtn.StatusP = Status.Good;}private void Button_Click2(object sender, RoutedEventArgs e){xbtn.StatusP = Status.Bad;}}
}
WPF Blend 自定义控件相关推荐
- (C# WPF Blend for Visual Studio 2019)设计一个刀剑神域风格的云盘客户端
"这虽然是游戏,但可不是闹着玩儿的" 前言 效果预览 WPF Blend入门之新建工程并修改为自定义窗口 控件样式模板 C#连接服务器端 前言 首先在这里先自己庆祝一下,历时接近一 ...
- [转]在WPF中自定义控件 UserControl
在这里我们将将打造一个UserControl(用户控件)来逐步讲解如何在WPF中自定义控件,并将WPF的一些新特性引入到自定义控件中来. 我们制作了一个带语音报时功能的钟表控件, 效果如下: 在VS中 ...
- 【转】在WPF中自定义控件
周银辉的开发博客(WPF) 在WPF中自定义控件(1) 一, 不一定需要自定义控件 在使用WPF以前,动辄使用自定义控件几乎成了惯性思维,比如需要一个带图片的按钮,但在WPF中此类任务却不需要如此大费 ...
- WPF让自定义控件不显示到工具箱
默认情况下,自义定控件是自动显示到工具箱中的,但是有时候对于一些自定义的控件,不想让它显示到工具箱中,那该怎么办呢?别急,Microsoft给我们提供了一个方法:ToolboxBrowsable,默认 ...
- WPF的自定义控件 依赖属性,DependencyProperty 路由事件RoutedEvent
起初,想添加TreeView的样式,在MouseOver时整行背景变色,但是我目前在XAML无法实现. 搜索了一下,有个方法在后台也要写代码. 参考:http://www.cnblogs.com/br ...
- 分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(4月16日-4月22日)
分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(4月16日-4月22日) 本周Silverlight学习资源更新 银光中国网友原创:Silverlight中获取 ...
- Silverlight/Windows8/WPF/WP7/HTML5周学习导读(8月5日-8月12日)
Silverlight/Windows8/WPF/WP7/HTML5周学习导读(8月5日-8月12日) 本周Silverlight学习资源更新 Silverlight设计时特性的使用 _eagle S ...
- 分享Silverlight/WPF/Windows Phone一周学习导读(8月22日-8月27日)
分享Silverlight/WPF/Windows Phone一周学习导读(8月22日-8月27日) 本周Silverlight学习资源更新: ASP.NET与Silverlight简单对比 aa54 ...
- 分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(3月12日-3月18日)
分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(3月12日-3月18日) 本周Silverlight学习资源更新 Silverlight实用窍门系列:58.Si ...
- My Bookmarks
新闻bbs类 欢迎莅临水木社区 126网易免费邮--你的专业电子邮局 网易126免费邮箱--专业电子邮局.容量自动翻倍,支持50兆附件,280M网盘.支持各种客户端软件收发,垃圾邮件拦截率超过98%. ...
最新文章
- getopt:命令行选项、参数处理
- SpringBoot 使用WebMvcConfigurer处理请求
- HDU2089——不要62 (数位DP)
- 新手站长必须养成的五个好习惯
- 时间机器(CDQ分治)
- office 2007启动要安装配置?
- 联想90W快充是假的吗?官方回应:我们不玩文字游戏
- 你属于开源性格测试六大分类中的哪一类呢
- jsp做看板_DataGear 使用静态HTML模板快速制作数据可视化看板
- Hexo NexT主题中添加百度分享功能
- 李雅普诺夫理论基础(1)
- Qt表格分页,跳转,首尾页
- 图片太大,导致页面加载过慢的处理方法
- 2022年5月22日-Taylor级数的python实验
- [todo] 如何高效工作
- 6-3 写字母(*) (10分)
- 2017 Multi-University Training Contest - Team 8:Fleet of the Eternal Throne(AC自动机)
- 【毕业设计/Matlab系列】基于PCM编译码和2ASK调制解调的通信系统仿真
- linux登录界面配置有趣的图案 /etc/motd,附带图片转字符串网址
- C#学习之面象对象继承练习(二)