在企业级应用中,通常我们会遇到这样的需求,需要点击一个按钮选择列表中的一项或者多项,然后将结果显示到按钮中。这里我给自己的控件命名为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. 正在载入中......loading页面的几种方法

    网页加载过程中提示“载入中…”,特别是使用动画效果,可以一个“等待”的温馨提示,用户体验很不错.下面介绍几种方法. 第一种: 原理就是,在网页载入时在页面最中间打入一个层上面显示,"网页正在 ...

  2. async语法升级踩坑小记

    从今年过完年回来,三月份开始,就一直在做重构相关的事情. 就在今天刚刚上线了最新一次的重构代码,希望高峰期安好,接近半年的Node.js代码重构. 包含从callback+async.waterfal ...

  3. GDB调试基础

    GDB调试基础 https://lesca.me/archives/gdb-basic-knowledge.html GDB笔记(二):条件断点.命令列表.监视点 https://lesca.me/a ...

  4. nginx配置location总结及rewrite规则写法【转】

    转自 nginx配置location总结及rewrite规则写法 | Sean's Noteshttp://seanlook.com/2015/05/17/nginx-location-rewrite ...

  5. Android studio 安装过程中遇到的问题

    之前用eclipse,想换下studio试试,安装时遇到问题,参考:http://www.cnblogs.com/csulennon/p/4178404.html

  6. Trie树子节点快速获取法

    今天做了一道leetcode上关于字典树的题:https://leetcode.com/problems/word-search-ii/#/description 一开始坚持不看别人的思路,完全自己写 ...

  7. python版本共存

    要玩多版本最好使用虚拟环境,避免根python切换及包误安装的麻烦 1.直接安装实现 1.1 windows下 到官网(https://www.python.org/downloads/)下载,如py ...

  8. (二) solr 索引数据导入:xml格式

    xml 是最常用的数据索引格式,不仅可以索引数据,还可以对文档与字段进行增强,从而改变它们的重要程度. 下面就是具体的实现方式: schema.xml的字段配置部分如下: <field name ...

  9. Django数据库数据表操作

    建立表单 django通过设置类来快速建表,打开models.py 例: from __future__ import unicode_literals from django.db import m ...

  10. NopCommerce Plugins 不能智能提示的解决方法(MVC 5 & RAZOR 3.0)

    分享给需要的朋友: http://mhammadchehab.com/wordpress/2013/12/enabling-intellisense-for-razor-in-class-librar ...