顯示具有 dialogs 標籤的文章。 顯示所有文章
顯示具有 dialogs 標籤的文章。 顯示所有文章

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; }

        }

    }

WPF學習筆記 - 14 Dialogs : MessageBox, OpenFileDialog, SaveFileDialog

MessageBox


MessageBox.Show("Content", "Title");

MessageBox.Show("This MessageBox has extra options.\n\nHello, world?", "My App", 

MessageBoxButton.YesNoCancel);

MessageBoxButton屬性:

OK

OKCancel

YesNoCancel

YesNo

接收使用者的reponse來執行不同的動作

MessageBoxResult result = MessageBox.Show("Would you like to greet the world with a \"Hello, 

world\"?", "My App", MessageBoxButton.YesNoCancel);

switch(result)

{

        case MessageBoxResult.Yes:

                MessageBox.Show("Hello to you too!", "My App");

                break;

        case MessageBoxResult.No:

                MessageBox.Show("Oh well, too bad!", "My App");

                break;

        case MessageBoxResult.Cancel:

                MessageBox.Show("Nevermind then...", "My App");

                break;

}

加上icon

MessageBox.Show("Hello, world!", "My App", MessageBoxButton.OK, MessageBoxImage.Information);

MessageBoxImage屬性:

Asterisk

Error

Exclamation

Hand

Information

None

Question

Stop

Warning

預設的選擇(預設選no)

MessageBox.Show("Hello, world?", "My App", MessageBoxButton.YesNo, MessageBoxImage.Question, 

MessageBoxResult.No);

OpenFileDialog


一個簡單的範例,選取txt後會把內容show在txtEditor裡

<DockPanel Margin="10">

        <WrapPanel HorizontalAlignment="Center" DockPanel.Dock="Top" Margin="0,0,0,10">

            <Button Name="btnOpenFile" Click="btnOpenFile_Click">Open file</Button>

        </WrapPanel>

        <TextBox Name="txtEditor" />

</DockPanel>

private void btnOpenFile_Click(object sender, RoutedEventArgs e)

{

                        OpenFileDialog openFileDialog = new OpenFileDialog(); 

// 設定Filter,指定只能開啟特定的檔案 

openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

                        // 選擇圖片的Filter

                        // openFileDialog.Filter = "Image files (*.png;*.jpeg)|*.png;*.jpeg|All files (*.*)|*.*";

                        if(openFileDialog.ShowDialog() == true)

                                txtEditor.Text = File.ReadAllText(openFileDialog.FileName);

}

設定預設從哪個目錄開始

openFileDialog.InitialDirectory = @"c:\temp\";

openFileDialog.InitialDirectory =

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

https://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

開啟複數檔案

<DockPanel Margin="10">

        <WrapPanel HorizontalAlignment="Center" DockPanel.Dock="Top" Margin="0,0,0,10">

            <Button Name="btnOpenFile" Click="btnOpenFiles_Click">Open files</Button>

        </WrapPanel>

        <ListBox Name="lbFiles" />

</DockPanel>

private void btnOpenFiles_Click(object sender, RoutedEventArgs e)

                {

                        OpenFileDialog openFileDialog = new OpenFileDialog();

                        openFileDialog.Multiselect = true; // 設定可以開多個檔案

                        openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

                        openFileDialog.InitialDirectory = 

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

                        if(openFileDialog.ShowDialog() == true)

                        {

                                foreach(string filename in openFileDialog.FileNames)

                                        lbFiles.Items.Add(Path.GetFileName(filename));

                        }

                }

SaveFileDialog


簡單的範例

<DockPanel Margin="10">

        <WrapPanel HorizontalAlignment="Center" DockPanel.Dock="Top" Margin="0,0,0,10">

            <Button Name="btnSaveFile" Click="btnSaveFile_Click">Save file</Button>

        </WrapPanel>

        <TextBox Name="txtEditor" TextWrapping="Wrap" AcceptsReturn="True" 

ScrollViewer.VerticalScrollBarVisibility="Auto" />

    </DockPanel>

private void btnSaveFile_Click(object sender, RoutedEventArgs e)

                {

                        SaveFileDialog saveFileDialog = new SaveFileDialog();

                        if(saveFileDialog.ShowDialog() == true)

                                File.WriteAllText(saveFileDialog.FileName, txtEditor.Text);

                }

設定Filter

saveFileDialog.Filter = "Text file (*.txt)|*.txt|C# file (*.cs)|*.cs";

設初始目錄

saveFileDialog.InitialDirectory = @"c:\temp\";

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

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