在企业级应用中,通常我们会遇到这样的需求,需要点击一个按钮选择列表中的一项或者多项,然后将结果显示到按钮中。这里我给自己的控件命名为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(用于列表页之类的选择)的更多相关文章

  1. 夺命雷公狗ThinkPHP项目之----企业网站13之文章列表页的实现(主要是分页的实现)

    列表页这个其实是比较简单的一个,直接遍历除数据即可: public function lists(){ //$mod = M("Article")->select(); // ...

  2. wpf企业应用之主从结构列表

    主从结构在企业级应用中相当常见,这里结合我的例子谈一下wpf中主从结构列表展示的常用做法,具体效果见 wpf企业级开发中的几种常见业务场景. 首先,Model有两种,主表对应model(假设为mode ...

  3. 夺命雷公狗ThinkPHP项目之----企业网站16之文章列表页的完善(关联查询)

    我们栏目的所属栏目不能总是以数字来显示吧??这样的话,估计老板会让您直接卷铺盖滚蛋噢,嘻嘻... 所以我们需要对她进行关联查询,控制器代码如下所示: public function lists(){ ...

  4. 夺命雷公狗ThinkPHP项目之----企业网站26之网站前台列表页的显示和完成分页功能

    我们用大I接收到我们get过来的栏目页的id然后通过文章的ar_cateid 来判断是不是属于该栏目下的,如果文章表ar_cateid = 栏目表的cate_id 那么就可以选出我们要查找的信息, 然 ...

  5. 夺命雷公狗ThinkPHP项目之----企业网站24之网站前台列表页面包屑导航的显示

    我们做面包屑导航的原理其实也是很简单的,我们的思路是: 首先找到该分类的id ,我们可以通过大 I来进行获取得到: 然后通过 大 D 方法让数据进入model层里面进行循环迭代查询, 当然,测试时候发 ...

  6. 夺命雷公狗ThinkPHP项目之----企业网站23之网站前台二级分类的跳转(URL跳转到列表页或产品页)

    我们现在开始做实现我们的二级菜单如何跳转到指定的列表页或者产品也呢?? 我们分享下数据库情况: 我们的数据库里提前给我们预留了一个cate_type的字段,那么我们可以让这个字段进行判断,从而遍历出指 ...

  7. 夺命雷公狗ThinkPHP项目之----企业网站18之网站配置列表页的完成

    我们点击下配置列表即可查看我们列表页的配置信息了: 其实这个最简单了,首先我们先来完成他控制器的代码: public function lists(){ $mod = M('Conf')->se ...

  8. 如何用PC标签在列表页中调出文章内容 phpcms

    如何用PC标签在列表页中调出文章内容 phpcms v9 moreinfo=”"参数说明 {pc:content action="lists" catid="$ ...

  9. dedecms讲解-arc.listview.class.php分析,列表页展示

    ./plus/list.php - 动态展示栏目列表页(也可能是频道封面)arc.listview.class.php 是dedecms的列表页的相关处理类__construct()         ...

随机推荐

  1. Linux基础-vim编辑器

    使用vi编辑器编辑文件/1.txt进入编辑模式写入内容“hello world” 命令行模式输入i,进入编辑模式 写入HelloWorld,按ESC进入命令行模式,输入:进入扩展模式输入wq保存退出 ...

  2. 【Loadrunner】LR参数化:利用mysql数据库里面的数据进行参数化

    很多同学都在自学loadrunner去做压力测试,但是如果要利用LR做压力测试,或者是其他工具,其中有一个环节是我们避开不了的,比如说:参数化 今天华华就给大家简要的介绍下,如果你要做的参数化的数据来 ...

  3. php中global和$GLOBALS最浅显易懂的解释

    官方文档: global指对变量的引用或者叫指针,$GLOBALS则是变量本身: $var1 = 1; $var2 = 2; function fun(){ $GLOBALS['var2'] = &a ...

  4. vi 编辑器使用技巧

    1.由命令"vi --version"所显示的内容知vi的全局配置文件 2.显示行号   ,非编辑模式输入 : set nu 3.显示颜色 1)在文件中找到 "synta ...

  5. 使用隐藏form表单下载文件,解决url方式下载,由于环境问题而限制url长度,满足不了所有的需求!

    一 对于某些环境导出是直接用wiondow.href=url直接导出下载,有些业务需求,如员工档案等字段比较多的时候,全选导出就会引发异常,由于Nginx转发长度限制的问题, 如果运维不愿意改变环境, ...

  6. aarch64_m3

    mrpt-stereo-camera-calibration-1.4.0-1.fc26.aarch64.rpm 2017-03-17 10:02 143K fedora Mirroring Proje ...

  7. MySQL增量备份与恢复实例【转】

    小量的数据库可以每天进行完整备份,因为这也用不了多少时间,但当数据库很大时,就不太可能每天进行一次完整备份了,这时候就可以使用增量备份.增量备份的原理就是使用了mysql的binlog日志.本次操作的 ...

  8. Discuz x3.2七牛远程附件设置

    一.DISCUZX2.5/3/3.1云存储通用接口1.1.0beta版本[8.22最新更新] 链接地址:http://www.discuz.net/thread-3399569-1-1.html 本帖 ...

  9. 关于bcb调用动态库,contains invalid OMF record, type 0x21 (possibly COFF)问题

    今天用C++Builder6.0 调用三方lib文件时,编译的时候出现如下错误: “contains invalid OMF record, type 0x21 (possibly COFF)” 才知 ...

  10. 转:vue-cli的webpack模板项目配置文件分析

    转载地址:http://blog.csdn.net/hongchh/article/details/55113751 一.文件结构 本文主要分析开发(dev)和构建(build)两个过程涉及到的文件, ...