Wpf enterprise application SelectButton (used for selection such as list pages), wpfselectbutton
In enterprise applications, we usually encounter such a requirement. You need to click a button to select one or more items in the list, and then display the result to the button. Here I name my control SelectButton. For details, see several common business scenarios in enterprise-level development of wpf.
My SelectButton is a user control that contains a Button and a TextBox. The Button is used to trigger the event and the TextBox is used to display the selected result. In addition, two dependency attributes SelectedItem and DisplayProperty are added to the control to bind the selected result and display it in TextBox. The following is the implementation code, hoping to inspire readers.
<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); } }}
Usage:
<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();}