文章出處

1.Menu菜單控件

 Exp1:

Code:

<Window x:Class="菜單Menu.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <Menu HorizontalAlignment="Left" Height="19" VerticalAlignment="Top" Width="167"><!--Menu控件下,每個子菜單都為MenuItem,Header可以設置顯示的內容-->
                <MenuItem Header="文件">
                    <MenuItem Header="打開">
                        <MenuItem Header="1.txt"></MenuItem>
                        <MenuItem Header="2.txt"></MenuItem>
                        <MenuItem Header="3.txt"></MenuItem>
                    </MenuItem>
                    <MenuItem Header="退出"></MenuItem>
                </MenuItem>
                <MenuItem Header="編輯">
                    <MenuItem Header="復制"></MenuItem>
                    <MenuItem Header="粘貼"></MenuItem>
                </MenuItem>
            </Menu>
        </StackPanel>
    </Grid>
</Window>
View Code

 

Exp2:

Code:

<Window x:Class="Menu菜單.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DockPanel><!--使用DockPanel布局,可使控件向上、下、左或右靠-->
            <Menu DockPanel.Dock="Top"><!--使Menu向上靠-->
                <MenuItem Header="文件">
                    <MenuItem Name="menuItemFirst" Header="1.txt" Click="menuItemFirst_Click"></MenuItem><!--向子菜單添加一個單擊事件-->
                    <MenuItem Header="2.txt"></MenuItem>
                    <MenuItem Header="3.txt"></MenuItem>
                </MenuItem>
                <MenuItem Header="編輯">
                    <MenuItem Header="復制"></MenuItem>
                    <MenuItem Header="粘貼"></MenuItem>
                </MenuItem>
            </Menu>
            <!--<TextBox TextWrapping="Wrap" DockPanel.Dock="Bottom"></TextBox>--><!--使TextBox向下靠-->
            <RichTextBox DockPanel.Dock="Bottom"></RichTextBox><!--使RichTextBox向下靠-->
        </DockPanel>
    </Grid>
</Window>
View Code
private void menuItemFirst_Click(object sender, RoutedEventArgs e)
 {
        MessageBox.Show("1.txt被點擊了");
}    
View Code

 知識點:

1)Menu默認會有一個Margin屬性值,如果想要按自己的方式顯示,最好重新設置,或刪除

2)Menu下的每個項都是MenuItem,其中,MenuItem下又可以設置MenuItem項,每個MenuItem項中,指定顯示的文本,應該可用Header屬性來設置

3)DockPanel布局(我自己解析為靠邊布局,Dock為碼頭的意思)可以使控件向上(Top)、下(Bottom)、左(Lfet)或右(Right)四個方向之一靠,例如設置方式是:DockPanel.Dock = "Top"向上靠

3)可為Menu或MenuItem指定名字,設置屬性為Name

 

2.ToolBar控件

 Exp:

 Code:

<Window x:Class="ToolBar控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DockPanel>
            <Menu DockPanel.Dock="Top"><!--Menu也是靠上靠,但如果大家都是往同一方向靠的話,就按順序來定了-->
                <MenuItem Header="文件"></MenuItem>
                <MenuItem Header="編輯"></MenuItem>
            </Menu>
            <ToolBar DockPanel.Dock="Top"><!--工具條控件,一般把常用的都放到工具條上面,而且上面可以放置很多的其他控件,不過,有些控件放進去之后,樣子會發生一些變化-->
                <!--<Button Content="保存"></Button>-->
                <!--可以通過設置Button的Content屬性顯示圖片,這樣工具條就不會顯得單調了-->
                <Button Height="30"><!--設置Button的content屬性有些特殊,可以不用Button.Content來設置,直接在Button標簽下設置,但其他控件 ,就都要指定哪一個屬性-->
                    <Image Source="images/2052.ico"></Image>
                </Button>
                <!--<Button Content="新建"></Button>-->
                <Button Height="30">
                    <Button.Content>
                        <Image Source="images/2063.ico"></Image>
                    </Button.Content>
                </Button>
                <CheckBox>自動保存</CheckBox>
            </ToolBar>
            <RichTextBox DockPanel.Dock="Bottom"></RichTextBox>
        </DockPanel>
    </Grid>
</Window>
View Code

 

知識點:

1)ToolBar控件中可以放置很多其他常用操作的控件,但有些控件放置到ToolBar里面后,樣子可能會有些改變

2)使用DockPanel靠邊布局的時候,如果出現有相同設置方向的情況時,就按控件添加的順序來顯示

3)Button控件的屬性Content比較特殊,可以再不指明的情況下,直接設置Content屬性的內容,而無需標明Button.Content,例如:

<Button>

  <Image Source = ... />

<Button>

<Button>

  <Button.Content>

    <Image Source = ... />

  </Button.Content>

<Button>

是一樣的,不過對于其他控件,就要標明哪一個屬性了

 

3.設置多窗口

 Exp:

Main.XAML

<Window x:Class="ToolBar控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowState="Maximized"
        Title="主窗口" Height="350" Width="525">
    <Grid>
        <DockPanel>
            <Menu DockPanel.Dock="Top"><!--Menu也是靠上靠,但如果大家都是往同一方向靠的話,就按順序來定了-->
                <MenuItem Header="文件"></MenuItem>
                <MenuItem Header="編輯"></MenuItem>
            </Menu>
            <ToolBar DockPanel.Dock="Top"><!--工具條控件,一般把常用的都放到工具條上面,而且上面可以放置很多的其他控件,不過,有些控件放進去之后,樣子會發生一些變化-->
                <!--<Button Content="保存"></Button>-->
                <!--可以通過設置Button的Content屬性顯示圖片,這樣工具條就不會顯得單調了-->
                <Button Height="30" Click="Button_Click"><!--設置Button的content屬性有些特殊,可以不用Button.Content來設置,直接在Button標簽下設置,但其他控件 ,就都要指定哪一個屬性-->
                    <Image Source="images/2052.ico"></Image>
                </Button>
                <!--<Button Content="新建"></Button>-->
                <Button Height="30">
                    <Button.Content>
                        <Image Source="images/2063.ico"></Image>
                    </Button.Content>
                </Button>
                <CheckBox>自動保存</CheckBox>
            </ToolBar>
            <!--<RichTextBox Name="richTextBox" DockPanel.Dock="Bottom"></RichTextBox>-->
            <TextBox Name="textBox" TextWrapping="Wrap" AcceptsReturn="True" DockPanel.Dock="Bottom"></TextBox>
            <!--AcceptsReturn設置可以支持回車換行,默認為false-->
        </DockPanel>
    </Grid>
</Window>
View Code

Main.XAML.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 ToolBar控件
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            AboutWindow aboutWindow = new AboutWindow();
            //aboutWindow.Txt = System.Windows.Markup.XamlWriter.Save(richTextBox.Document);
            //string s = System.Windows.Markup.XamlWriter.Save(richTextBox.Document).ToString();
            aboutWindow.Txt = textBox.Text;
            aboutWindow.ShowDialog();//打開窗口
        }
    }
}
View Code

About.XAML

<Window x:Class="ToolBar控件.AboutWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="關于" Height="300" Width="300" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded"><!--Title修改窗口的標題為‘關于’,ResizeMode修改窗口的最小化、最大化,WindowStartupLocation窗口顯示的初始位置-->
    <Grid>
        <TextBlock Name="textBlock" Text="第一個新建的子窗口"></TextBlock>
    </Grid>
</Window>
View Code

About.XAML.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;

namespace ToolBar控件
{
    /// <summary>
    /// AboutWindow.xaml 的交互邏輯
    /// </summary>
    public partial class AboutWindow : Window
    {
        public string Txt { get; set; }
        public AboutWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            textBlock.Text = Txt;
        }
    }
}
View Code

知識點:

1)WindowState設置窗體的初始狀態(最大化,最小化)

   Titile設置窗體的顯示標題

 ResizeMode設置窗體初始時的是否顯示最大化、最小化(是否顯示最大化、是否顯示最小化)

 WindowStartupLocation窗體初始時的顯示位置(窗體居中,默認)

 

 

 

 2.aboutWindow.ShowDialog() 打開窗口

 3.如果把一個窗口A中的值傳到另外一個窗口B,可以在B中設置屬性,然后在A中定義B的對象后,直接調用即可

(無論是把主窗體的值傳遞給子窗體,還是把子窗體的值傳遞給主窗體,都可以運用設置屬性的方法)

        

 4.通過修改App.xaml文件,指定程序啟動時哪一個窗體最先打開

...

 StartupUri="MainWindow.xaml"

...

 

 5.TextBox中通過設置屬性AcceptsReturn,設置是否支持回車換行,默認是false

 

 4.窗口間傳值

1. 窗口鍵傳值,可以通過設置屬性來實現

2.如果窗口使用ShowDialog打開的,則給DialogResult賦值會自動關閉窗口,并且把DialogResult屬性的通過ShowDialog方法的返回值返回

 

Code:

Main.XAML

<Window x:Class="窗口間傳值.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="主窗口" Height="350" Width="525">
    <Grid>
        <Button Name="btnTest" Content="窗口間傳值" HorizontalAlignment="Left" Margin="100,65,0,0" VerticalAlignment="Top" Width="139" Click="btnTest_Click"/>

    </Grid>
</Window>
View Code

Main.XALM.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 窗口間傳值
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            InputWindow inputWindow = new InputWindow();
            bool? b = inputWindow.ShowDialog();
            if (b == null)
            {
                MessageBox.Show("沒有輸入值!");
            }
            else if (b == true)
            {
                MessageBox.Show("確定:" + inputWindow.InputValues);
            }
            else
            {
                MessageBox.Show("取消");
            }
            
        }
    }
}
View Code

 InpoutWindow.XAML

<Window x:Class="窗口間傳值.InputWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterScreen"
        ResizeMode="NoResize"
        Title="輸入窗體" Height="150" Width="300">
    <Grid>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBox Name="txtInput" Grid.ColumnSpan="2"></TextBox>
            <Button Name="btnOK" Content="確定" Grid.Row="1" Margin="10" Click="btnOK_Click"></Button>
            <Button Name="btnCancel" Content="取消" Grid.Row="1" Grid.Column="1" Margin="10" Click="btnCancel_Click"></Button>
        </Grid>
    </Grid>
</Window>
View Code

InputWindow.XAML.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;

namespace 窗口間傳值
{
    /// <summary>
    /// InputWindow.xaml 的交互邏輯
    /// </summary>
    public partial class InputWindow : Window
    {
        public string InputValues { get; set; }
        public InputWindow()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            InputValues = txtInput.Text;
            DialogResult = true;//如果窗口使用ShowDialog打開的,則給DialogResult賦值會自動關閉窗口,
                                //并且把DialogResult屬性的通過ShowDialog方法的返回值返回
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
    }
}
View Code

 

5.OpenFileDialog打開文件對話框

 Code:

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            //打開文件對話框
            OpenFileDialog ofd = new OpenFileDialog();//打開文件對話框
            //ofd.InitialDirectory設置默認打開的文件目錄
            ofd.Filter = "文本文件|*.txt|圖片文件|*.jpg;*.png;*.JPEG|所有文件|*.*";//設置過濾器,前兩個為一組,以“|”問分割線,前一個是要顯示的內容,后一個是文件的類型,如果文件的類型有多個時,可用“;”分隔
            //注意:Fileter屬性的設置,應該是在ShowDialog()方法執行前才進行設置
            if (ofd.ShowDialog() == true)
            {
                string s = ofd.FileName;//打開的文件名
                MessageBox.Show("打開文件:" + s);
            }
            else
            {
                MessageBox.Show("取消");
            }
        }
View Code

 

知識點:

1)需引用命名空間:Microsoft.Win32;

2)ShowDialog()方法打開對話框

3)InitialDirectory屬性設置默認打開的文件目錄

4)Filter屬性設置過濾器。設置過濾器時,前兩個為一組,以“|”問分割線,前一個是要顯示的內容,后一個是文件的類型,如果文件的類型有多個時,可用“;”分隔

 注意:Fileter屬性的設置,應該是在ShowDialog()方法執行前才進行設置

5)FileName為打開的文件名

 

 6.SaveFileDialog保存文件對話框

 Code:

  private void btnSaveFile_Click(object sender, RoutedEventArgs e)
        {
            //保存文件對話框
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "文本文件|*.txt|圖片文件|*.jpg;*.png;*.JPEG|所有文件|*.*";
            if (sfd.ShowDialog() == true)
            {
                MessageBox.Show("保存文件" + sfd.SafeFileName);
            }
        }
View Code
private void btnShowImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "圖片|*.jpg;*.png;*.JPEG";
            if (ofd.ShowDialog() == true)
            {
                string fileName = ofd.FileName;
                imageControl.Source = new BitmapImage(new Uri(fileName));//用代碼設置Image控件的Source屬性
            }
        }
View Code

 

知識點:

1)ShowDialog()方法顯示保存文件對話框

2)Filter 屬性設置過濾器

3)SafeFileName屬性為保存文件名

4)用代碼設置Image控件的Source屬性: imageControl.Source = new BitmapImage(new Uri(fileName))

 

 

 

 

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()