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() ...
随机推荐
- [转]Laplace算子和Laplacian矩阵
1 Laplace算子的物理意义 Laplace算子的定义为梯度的散度. 在Cartesian坐标系下也可表示为: 或者,它是Hessian矩阵的迹: 以热传导方程为例,因为热流与温度的梯度成正比,那 ...
- Java中关于变量的几种情况
Java中关于变量的几种情况 1.继承时变量的引用关系 class Animals { int age = 10; void enjoy() { System.out.println("An ...
- 7 SQL优化技术
7.1 改变访问结构 7.2 修改SQL语句 SELECT deptno FROM dept WHERE deptno NOT IN (SELECT deptno FROM emp); SELEC ...
- python(32):多进程(2) multiprocessing
python 多线程:多线程 由于Python设计的限制(我说的是咱们常用的CPython).最多只能用满1个CPU核心. Python提供了非常好用的多进程包multiprocessing,你只需要 ...
- 读书笔记 effective c++ Item 48 了解模板元编程
1. TMP是什么? 模板元编程(template metaprogramming TMP)是实现基于模板的C++程序的过程,它能够在编译期执行.你可以想一想:一个模板元程序是用C++实现的并且可以在 ...
- 下一代Android打包工具,100个渠道包只需要10秒钟 https://github.com/mcxiaoke
https://github.com/mcxiaoke/packer-ng-plugin https://github.com/Meituan-Dianping/walle https://githu ...
- IE手工导入证书
打开cer文件->欢迎使用证书导入向导->下一步->将所有的证书放入下列存储->受信任的根证书颁发机构->完成
- luoguP2735 电网 Electric Fences
一道校内模拟赛遇见的题 ** 不会正解就真的很麻烦的 数学题 ** 有一种东西叫 皮克定理 发现的千古神犇: 姓名:George Alexander Pick(所以叫皮克定理呀 国籍:奥地利(蛤!竟然 ...
- .net程序员写业务代码需要注意的地方
代码规范要求1.命名空间规范:dao层的impl实现和接口采用一样的命名空间,到对应文件夹层:IxxDaoContext与其实现类采用顶级命名空间. 2.TableEntity文件夹:所有的实体放到各 ...
- C/C++经典面试题
1.指向数组的指针 和 指向数组首元素的指针 2018-03-07 昨天在牛客上看到这么一道C语言面试题,挺经典的,特来分享给大家. 程序如下,问输出结果 #include <stdio.h&g ...