专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WPF

wpf企业应用之SelectButton(用以列表页之类的选择)

发布时间:2011-06-23 13:56:29 文章来源:www.iduyao.cn 采编人员:星星草
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="5" Grid.Row="3" Grid.Column="1"/>
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();
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: