在企业级应用中,通常我们会遇到这样的需求,需要点击一个按钮选择列表中的一项或者多项,然后将结果显示到按钮中。这里我给自己的控件命名为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(); }
wpf企业应用之SelectButton(用于列表页之类的选择)
原文:http://www.cnblogs.com/Fuss/p/4095585.html