wpf企业应用之SelectButton(用于列表页之类的选择)
在企业级应用中,通常我们会遇到这样的需求,需要点击一个按钮选择列表中的一项或者多项,然后将结果显示到按钮中。这里我给自己的控件命名为SelectButton,具体效果见 wpf企业级开发中的几种常见业务场景。
我的SelectButton是个用户控件,里面包含一个Button和一个TextBox,Button用于触发事件,TextBox用来显示选择后的结果。另外控件中添加了两个依赖项属性SelectedItem和DisplayProperty用来绑定选择后的结果,然后在TextBox中显示。下面是实现代码,希望能对读者有一定启发作用。
<UserControl x:Class="Fuss.Wpf.Controls.SelectButton"
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">
<DockPanel Name="selector_Panel" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Button Name="selector_Button" DockPanel.Dock="Right" VerticalAlignment="Stretch" IsEnabled="{Binding IsEnabled, ElementName=selector_Panel}">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="button_Border" Background="{StaticResource Common_GradientBackgroundColor}" BorderBrush="{StaticResource Common_SolidBordColor}" BorderThickness="1">
<StackPanel Orientation="Horizontal">
<Ellipse Width="4" Height="4" Margin="1" Fill="Blue" VerticalAlignment="Center"/>
<Ellipse Width="4" Height="4" Margin="1" Fill="Blue" VerticalAlignment="Center"/>
<Ellipse Width="4" Height="4" Margin="1" Fill="Blue" VerticalAlignment="Center"/>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter TargetName="button_Border" Property="Background" Value="{StaticResource Common_MouseMove_GradientBackgroundColor}"/>
<Setter TargetName="button_Border" Property="BorderBrush" Value="{StaticResource Common_MouseMove_SolidBordColor}"/>
</Trigger>
<Trigger Property="Button.IsPressed" Value="True">
<Setter TargetName="button_Border" Property="Background" Value="{StaticResource Common_MouseDown_RadialGradientBackgroundColor}"/>
<Setter TargetName="button_Border" Property="BorderBrush" Value="{StaticResource Common_MouseDown_SolidBordColor}"/>
</Trigger>
<Trigger Property="Button.IsEnabled" Value="False">
<Setter TargetName="button_Border" Property="Background" Value="{StaticResource Common_DisabledSolidBackgroundColor}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
<TextBox Name="selector_TextBox" Margin="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" IsReadOnly="True" IsEnabled="{Binding IsEnabled, ElementName=selector_Panel}"/>
</DockPanel>
</UserControl>
using System;
using System.Collections.Generic;
using System.ComponentModel;
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 Fuss.Wpf.Controls
{
public partial class SelectButton : UserControl
{
public SelectButton()
{
InitializeComponent();
this.Loaded += SelectButton_Loaded;
} public Object SelectedItem
{
get
{
return (Object)GetValue(SelectedItemProperty);
}
set
{
SetValue(SelectedItemProperty, value);
if (value != null)
{
var pro = value.GetType().GetProperty(DisplayProperty);
if (pro != null && pro.GetValue(value) != null)
selector_TextBox.Text = pro.GetValue(value).ToString();
else
selector_TextBox.Text = "";
}
}
} public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(Object), typeof(SelectButton),
new FrameworkPropertyMetadata(String.Empty, new PropertyChangedCallback(OnSelectedItemChanged))
{
BindsTwoWayByDefault = true
}); private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var selectButton = d as SelectButton;
selectButton.SelectedItem = e.NewValue;
} public String DisplayProperty
{
get
{
return (String)GetValue(DisplayPropertyProperty);
}
set
{
SetValue(DisplayPropertyProperty, value);
}
}
public static readonly DependencyProperty DisplayPropertyProperty =
DependencyProperty.Register("DisplayProperty", typeof(String), typeof(SelectButton), new PropertyMetadata("")); public event EventHandler Click; void SelectButton_Loaded(object sender, RoutedEventArgs e)
{
selector_Button.Click += selector_Button_Click;
} void selector_Button_Click(object sender, RoutedEventArgs e)
{
if (this.Click != null)
Click(this, EventArgs.Empty);
} }
}
用法如下
<customer:SelectButton x:Name="SelectButton_ProductPlan" SelectedItem="{Binding ProductPlan}" DisplayProperty="Num" Click="SelectButton_ProductPlan_Click" Margin="" Grid.Row="" Grid.Column=""/>
private void SelectButton_ProductPlan_Click(object sender, EventArgs e)
{
ProductPlanSelectionWindow win = new ProductPlanSelectionWindow(VM.StockProduct);
win.Owner = Window.GetWindow(this);
win.SelectComplete += (s1, e1) =>
{
VM.ProductPlan = s1 as tb_productplan;
};
win.ShowDialog();
}
wpf企业应用之SelectButton(用于列表页之类的选择)的更多相关文章
- 夺命雷公狗ThinkPHP项目之----企业网站13之文章列表页的实现(主要是分页的实现)
列表页这个其实是比较简单的一个,直接遍历除数据即可: public function lists(){ //$mod = M("Article")->select(); // ...
- wpf企业应用之主从结构列表
主从结构在企业级应用中相当常见,这里结合我的例子谈一下wpf中主从结构列表展示的常用做法,具体效果见 wpf企业级开发中的几种常见业务场景. 首先,Model有两种,主表对应model(假设为mode ...
- 夺命雷公狗ThinkPHP项目之----企业网站16之文章列表页的完善(关联查询)
我们栏目的所属栏目不能总是以数字来显示吧??这样的话,估计老板会让您直接卷铺盖滚蛋噢,嘻嘻... 所以我们需要对她进行关联查询,控制器代码如下所示: public function lists(){ ...
- 夺命雷公狗ThinkPHP项目之----企业网站26之网站前台列表页的显示和完成分页功能
我们用大I接收到我们get过来的栏目页的id然后通过文章的ar_cateid 来判断是不是属于该栏目下的,如果文章表ar_cateid = 栏目表的cate_id 那么就可以选出我们要查找的信息, 然 ...
- 夺命雷公狗ThinkPHP项目之----企业网站24之网站前台列表页面包屑导航的显示
我们做面包屑导航的原理其实也是很简单的,我们的思路是: 首先找到该分类的id ,我们可以通过大 I来进行获取得到: 然后通过 大 D 方法让数据进入model层里面进行循环迭代查询, 当然,测试时候发 ...
- 夺命雷公狗ThinkPHP项目之----企业网站23之网站前台二级分类的跳转(URL跳转到列表页或产品页)
我们现在开始做实现我们的二级菜单如何跳转到指定的列表页或者产品也呢?? 我们分享下数据库情况: 我们的数据库里提前给我们预留了一个cate_type的字段,那么我们可以让这个字段进行判断,从而遍历出指 ...
- 夺命雷公狗ThinkPHP项目之----企业网站18之网站配置列表页的完成
我们点击下配置列表即可查看我们列表页的配置信息了: 其实这个最简单了,首先我们先来完成他控制器的代码: public function lists(){ $mod = M('Conf')->se ...
- 如何用PC标签在列表页中调出文章内容 phpcms
如何用PC标签在列表页中调出文章内容 phpcms v9 moreinfo=”"参数说明 {pc:content action="lists" catid="$ ...
- dedecms讲解-arc.listview.class.php分析,列表页展示
./plus/list.php - 动态展示栏目列表页(也可能是频道封面)arc.listview.class.php 是dedecms的列表页的相关处理类__construct() ...
随机推荐
- C# IsAssignableFrom与IsSubClassOf 判断匿名类是否继承父类
public class Dog : Animal { public string name { get; set; } } public class Animal { public string i ...
- spfa+floyed+最长路+差分约束系统(F - XYZZY POJ - 1932)(题目起这么长感觉有点慌--)
题目链接:https://cn.vjudge.net/contest/276233#problem/F 题目大意:给你n个房子能到达的地方,然后每进入一个房子,会消耗一定的生命值(有可能是负),问你一 ...
- 网络流最大流(拆点)(附带kuangbin最大流模板)(目测这个题有bug)
题目链接:https://vjudge.net/contest/68128#problem/H 我觉得这个题有bug,如果饮料和食物都为0,但是同时有五个人什么也不需要,按道理来讲,最多受益的人数为5 ...
- golang的json序列化
json就是简单的数据交换格式,语法类似javascript的对象和列表,是最常见的后端和运行在网页上的js之间的通信格式. encoding: 编码json数据需要使用到Marshal()函数. f ...
- ip_local_deliver && ip_local_deliver_finish
当ip包收上来,查路由,发现是发往本地的数据包时,会调用ip_local_deliver函数: ip_local_deliver中对ip分片进行重组,经过LOCAL_IN钩子点,然后调用ip_loca ...
- Nodejs 发送邮件
var nodemailer = require("nodemailer");var mailTitle='http://bemupa.forumieren.com:Best Mu ...
- vue全面介绍--全家桶、项目实例
简介 “简单却不失优雅,小巧而不乏大匠”. 2016年最火的前端框架当属Vue.js了,很多使用过vue的程序员这样评价它,“vue.js兼具angular.js和react.js的优点,并剔除了它们 ...
- CF 983B 序列函数
CF 983B 序列函数 一道本校神仙wucstdio出的毒瘤签到题. 题意: 给你一段序列,求出它们的最大异或和. 解法: 其实这道题并不很难,但读题上可能会有困难. 其实样例我是用Python 3 ...
- Centos之压缩和解压缩命令
常用压缩格式:.zip .gz .bz2 常用压缩格式:.tar.gz .tar.bz2 zip格式压缩 zip压缩文件名 源文件 压缩文件 zip -r 压缩文件名 源目录 压缩目录 [root@ ...
- (一)问候 HttpClient
第一节: HttpClient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并 ...