Show toolbar

2013年1月31日 星期四

C# WPF Confirm Options

標題:使用ShowDialog顯示Confirm視窗並回傳RadioButton的Option名稱
XAML (MainWindow.xaml):
<Window x:Class="WPFConfirmOptions.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="200">
    <Grid>
        <Button Content="Confirm Option" Name="confirmBtn" Click="confirmBtn_Click" />
    </Grid>
</Window>

C# (MainWindow.xaml.cs):
using System;
using System.Windows;
using System.Collections.Generic; //List

namespace WPFConfirmOptions
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void confirmBtn_Click(object sender, RoutedEventArgs e)
        {
            var options = new List<Confirm.Option>();
            options.Add(new Confirm.Option
            {
                Name = "Test1",
                Description = "Test Description 1."
            });
            options.Add(new Confirm.Option
            {
                Name = "Test2",
                Description = "Test Description 2."
            });
            options.Add(new Confirm.Option
            {
                Name = "Test3",
                Description = "Test Description 3."
            });
            options.Add(new Confirm.Option
            {
                Name = "Test4",
                Description = "Test Description 4."
            });
            options.Add(new Confirm.Option
            {
                Name = "Test5",
                Description = "Test Description 5."
            });
            Confirm confirm = new Confirm("Confirm", "Hello World", options);
            confirm.ShowDialog();
            if (confirm.DialogResult.HasValue && confirm.DialogResult.Value)
            {
                System.Windows.MessageBox.Show(confirm.CheckedName.ToString());
            }
        }
    }
}

XAML (Confirm.xaml):
<Window x:Class="WPFConfirmOptions.Confirm"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Name="Windows" Title="Confirm" Height="230" Width="300">
    <Grid>
        <TextBlock Height="20" HorizontalAlignment="Center" Margin="0,10,0,0" Name="tbk_StlTypeTitle" Text="Confirm" VerticalAlignment="Top" Width="120" TextAlignment="Center" />
        <GroupBox Header="Options" Margin="0,30,0,50" Name="gbx">
            <Grid Name="OptionsGrid"></Grid>
        </GroupBox>
        <Grid Height="50" Width="240" HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <Button Content="OK" Height="30" Width="100" Name="btn_OK" VerticalAlignment="Center" HorizontalAlignment="Left" Click="btn_OK_Click" />
            <Button Content="Cancel" Height="30" Width="100" Name="btn_Cancel" VerticalAlignment="Center" HorizontalAlignment="Right" Click="btn_Cancel_Click" />
        </Grid>
    </Grid>
</Window>

C# (Confirm.xaml.cs):
using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic; //List
using System.Linq;

namespace WPFConfirmOptions
{
    /// <summary>
    /// Interaction logic for StlTypeConfirm.xaml
    /// </summary>
    public partial class Confirm : Window
    {
        public Confirm(string header, string message, List<Option> options)
        {
            InitializeComponent();
            this.Header = header;
            this.Message = message;
            this.Options = options;
            AddRadioButton();
        }

        public string Header //Set Head Title
        {
            set
            {
                Windows.Title = value;
            }
        }

        public string Message //Set Content Description
        {
            set
            {
                tbk_StlTypeTitle.Text = value;
            }
        }

        public List<Option> Options;

        public String CheckedName = "";

        public String ResultName
        {
            get
            {
                return CheckedName;
            }
        }

        public class Option
        {
            public string Name { get; set; }
            public string Description { get; set; }
        }

        public void AddRadioButton()
        {
            int key = 0;
            foreach (Option value in Options) // Create Options
            {
                RadioButton radioButton = new RadioButton();
                radioButton.Name = value.Name;
                radioButton.Content = value.Description;
                radioButton.Height = 20;
                radioButton.Width = 250;
                radioButton.Margin = new Thickness(0, (20 + 30 * key), 0, 0);
                radioButton.HorizontalAlignment = HorizontalAlignment.Center;
                radioButton.VerticalAlignment = VerticalAlignment.Top;
                radioButton.IsChecked = false;
                this.OptionsGrid.Children.Add(radioButton);
                this.RegisterName(radioButton.Name, radioButton);
                key++;
            }
            Windows.Height = 200 + 30 * key;
            return;
        }

        private void btn_OK_Click(object sender, RoutedEventArgs e)
        {
            foreach (Option value in Options)
            {
                RadioButton radiobutton = (RadioButton)OptionsGrid.FindName(value.Name);
                if (radiobutton.IsChecked == true)
                {
                    CheckedName = radiobutton.Name;
                    DialogResult = true;
                    return;
                }
            }
            System.Windows.MessageBox.Show("No Select.");
        }

        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            return;
        }
    }
}

結果:

說明:
使用Add Window方式設計WPF Confirm視窗, 主視窗可設定加入多組選項包含名稱及描述, 並在ShowDialog後的Confirm視窗產生標題、說明及選項, 最後按下確認回傳RadioButton的Option名稱。

沒有留言:

張貼留言