wpfエンタープライズアプリケーションのSelectButton(リストページなどの選択に使用)

18309 ワード

エンタープライズアプリケーションでは、通常、1つのボタンをクリックしてリストの1つ以上を選択し、結果をボタンに表示する必要があります.ここで私は自分のコントロールにSelectButtonという名前を付けました.具体的な効果はwpfエンタープライズ開発におけるいくつかの一般的なビジネスシーンを参照してください.
私のSelectButtonはユーザーコントロールで、ButtonとTextBoxが含まれています.Buttonはイベントをトリガーし、TextBoxは選択した結果を表示します.また、コントロールには、選択した結果をバインドする2つの依存属性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();

}