顯示具有 custom dialog 標籤的文章。 顯示所有文章
顯示具有 custom dialog 標籤的文章。 顯示所有文章

2015年5月30日 星期六

WPF學習筆記 - 15 Custom dialog

按下Enter name...後跳出自訂的dialog,輸入name後,會把值帶到lblName裡

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

        <TextBlock>Hello, world. My name is:</TextBlock>

        <TextBlock Name="lblName" Margin="0,10" TextAlignment="Center" FontWeight="Bold">[No 

name entered]</TextBlock>

        <Button Name="btnEnterName" Click="btnEnterName_Click">Enter name...</Button>

</StackPanel>

private void btnEnterName_Click(object sender, RoutedEventArgs e)

                {

                        Dialogs inputDialog = new Dialogs("Please enter your name:", "Elliott Chen");

                        if(inputDialog.ShowDialog() == true)

                                lblName.Text = inputDialog.Answer;

                }

新增Dialogs.xaml,

拿掉Hight和Width屬性,改為SizeToContent="WidthAndHeight",讓視窗的大小自動調整,

WindowStartupLocation="CenterScreen"(或CenterOwner),屬性設定視窗被打開時的起始位置,

另外在Cancel button加上IsCancel="True",確保使用者按下此button或Esc會關閉視窗,

IsDefault="True",會把focus指給此button,並按下Enter可以執行此button

<Window x:Class="WpfApplication1.Dialogs"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Input" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen"

        ContentRendered="Window_ContentRendered"> // 這裡取消了Hight和Width,新增了其他的屬性

    <Grid Margin="15">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="Auto" />

            <ColumnDefinition Width="*" />

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

        <Label Name="lblQuestion" Grid.Column="1">Question:</Label>

        <TextBox Name="txtAnswer" Grid.Column="1" Grid.Row="1" MinWidth="250">Answer</TextBox>

        <WrapPanel Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Right" Margin="0,15,0,0">

            <Button IsDefault="True" Name="btnDialogOk" Click="btnDialogOk_Click" MinWidth="60" 

Margin="0,0,10,0">_Ok</Button>

            <Button IsCancel="True" MinWidth="60">_Cancel</Button>

        </WrapPanel>

    </Grid>

</Window>

backend


在constructor多加兩個參數,可以傳入問題和回答,

btnDialogOk_Click method只是傳回一個result,告知WPF,

需要注意的是Window_ContentRendered事件,自動選取txtAnswer的內容,並focuse,

最後的Answer method將txtAnswer裡內容傳回去

// 與一般的作法幾乎一模一樣

public partial class Dialogs : Window

    {

        public Dialogs(string question, string defaultAnswer = "")

        {

            InitializeComponent();

            lblQuestion.Content = question;

            txtAnswer.Text = defaultAnswer;

        }

        private void btnDialogOk_Click(object sender, RoutedEventArgs e)

        {

            this.DialogResult = true;

        }

        private void Window_ContentRendered(object sender, EventArgs e)

        {

            txtAnswer.SelectAll();

            txtAnswer.Focus();

        }

        public string Answer

        {

            get { return txtAnswer.Text; }

        }

    }

[泰國] 芭達雅大城八日遊 Day7、8 - 瑪哈泰寺、Wat Ratchaburana、帕蒙空博碧寺、帕席桑碧寺、Hua Ro market、Chantharakasem National Museum、Amporn Department Store

第七天行程,早上吃飯店早餐,算不錯吃,只是有螞蟻,但吃完沒拉,所以應該還好 吃飽後飽嘟嘟車,一小時 200 泰銖,比自己叫還便宜,終於要來世界遺產之旅了 第一站:瑪哈泰寺,應該是大城最有名的景點了,入門票大人 120 ,小孩免費,其實不便宜 大城指標性景點...