1. 程式人生 > >UWP入門(八)--幾個簡單的控制元件

UWP入門(八)--幾個簡單的控制元件

每天看幾個,要不聊幾天我就可以看完啦,加油!

看效果

這裡寫圖片描述

1. CheckBox

      <TextBlock Grid.Row="0" Text="CheckBox" VerticalAlignment="Center" />
        <StackPanel Grid.Column="1"
                    Margin="20,10,0,10" 
                    Orientation="Horizontal">
            <CheckBox Name="MyCheckBox" 
                      Content
="Agree?" Tapped="MyCheckBox_Tapped" />
<TextBlock Name="CheckBoxResultTextBlock" /> </StackPanel>
  private void MyCheckBox_Tapped(object sender, TappedRoutedEventArgs e)
        {
            CheckBoxResultTextBlock.Text = MyCheckBox.IsChecked.ToString();
        }

2. RadioButton

 <TextBlock Grid.Row="2" 
                   Text="RadioButton"  
                   VerticalAlignment="Center" />
        <StackPanel Grid.Row="2" 
                    Grid.Column="1" 
                    Orientation="Horizontal"
                    Margin="20,10,0,10">
            <RadioButton
Name="YesRadioButton" Content="Yes" GroupName="MyGroup" Checked="RadioButton_Checked" /> <RadioButton Name="NoRadioButton" Content="No" GroupName="MyGroup" Checked="RadioButton_Checked" /> <TextBlock Name="RadioButtonTextBlock" /> </StackPanel>
 private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButtonTextBlock.Text = (bool)YesRadioButton.IsChecked ? "Yes" : "No";
        }

3. CombomBox

 <TextBlock Grid.Row="3" 
                   Text="ComboBox" 
                   Name="MyComboBox"  
                   VerticalAlignment="Center" />
        <StackPanel Orientation="Horizontal" 
                    Grid.Row="3" 
                    Grid.Column="1" 
                    Margin="20,10,0,10">
            <ComboBox SelectionChanged="ComboBox_SelectionChanged" >
                <ComboBoxItem Content="Fourth" />
                <ComboBoxItem Content="Fifth" />
                <ComboBoxItem Content="Sixth" IsSelected="True" />
            </ComboBox>
            <TextBlock Name="ComboBoxResultTextBlock" />
        </StackPanel>
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBoxResultTextBlock == null) return;

            var combo = (ComboBox)sender;
            var item = (ComboBoxItem)combo.SelectedItem;
            ComboBoxResultTextBlock.Text = item.Content.ToString();
        }

4. ListBox

  <TextBlock Grid.Row="4" Text="ListBox" VerticalAlignment="Center" />
        <StackPanel Grid.Row="4" Grid.Column="1"  Margin="20,10,0,10">
            <ListBox Name="MyListBox" 
                     SelectionMode="Multiple" 
                     SelectionChanged="ListBox_SelectionChanged">
                <ListBoxItem Content="First" />
                <ListBoxItem Content="Second" />
                <ListBoxItem Content="Third" />
            </ListBox>
            <TextBlock Name="ListBoxResultTextBlock" />
        </StackPanel>
       private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedItems = MyListBox.Items.Cast<ListBoxItem>()
                                  .Where(p => p.IsSelected)
                                    .Select(t => t.Content.ToString())
                                      .ToArray();

            ListBoxResultTextBlock.Text = string.Join(", ", selectedItems);

        }

5. image

 <TextBlock Grid.Row="5" Text="Image" VerticalAlignment="Center" />
        <Image Source="Assets/StoreLogo.png" 
               HorizontalAlignment="Left"
               Width="250"
               Height="50"
               Grid.Row="5" 
               Grid.Column="1" 
               Stretch="Uniform"
               Margin="20,10,0,10" />

image 的四種拉伸方法

  • None
    • 不做任何處理,一般比較大
  • Fill
    • 佔據所給的最大空間,比例會失調
  • Uniform
    • 按比例伸縮,佔據所給的最大空間
  • UniformFill
    • 按比例伸縮,佔據大小

6. 漂亮的 ToggleSwitch

<TextBlock Grid.Row="8" 
                   Text="ToggleSwitch" 
                   VerticalAlignment="Center" />
        <StackPanel Grid.Row="8" 
                      Grid.Column="1"  
                      Margin="20,10,0,10" >
            <ToggleSwitch>
                <ToggleSwitch.OffContent>
                    <TextBlock Text="I'm off right now." />
                </ToggleSwitch.OffContent>
                <ToggleSwitch.OnContent>
                    <TextBlock Text="I'm on!" />
                </ToggleSwitch.OnContent>
            </ToggleSwitch>
        </StackPanel>

不需要程式碼

7. ToggleButton

<TextBlock Grid.Row="7" Text="ToggleButton" VerticalAlignment="Center"  />
        <StackPanel Orientation="Horizontal" 
                    Grid.Row="7" 
                    Grid.Column="1"  
                    Margin="20,10,0,10" >
            <ToggleButton Name="MyToggleButton" 
                          Content="Premium Option" 
                          IsThreeState="True" 
                          Click="MyToggleButton_Click" />
            <TextBlock Name="ToggleButtonResultTextBlock" />
        </StackPanel>
 private void MyToggleButton_Click(object sender, RoutedEventArgs e)
        {
            ToggleButtonResultTextBlock.Text = MyToggleButton.IsChecked.ToString();
        }

程式碼

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10,10,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Text="CheckBox" VerticalAlignment="Center" />
        <StackPanel Grid.Column="1"
                    Margin="20,10,0,10" 
                    Orientation="Horizontal">
            <CheckBox Name="MyCheckBox" 
                      Content="Agree?"
                      Tapped="MyCheckBox_Tapped" />
            <TextBlock Name="CheckBoxResultTextBlock" />
        </StackPanel>

        <TextBlock Grid.Row="2" 
                   Text="RadioButton"  
                   VerticalAlignment="Center" />
        <StackPanel Grid.Row="2" 
                    Grid.Column="1" 
                    Orientation="Horizontal"
                    Margin="20,10,0,10">
            <RadioButton Name="YesRadioButton" 
                         Content="Yes" 
                         GroupName="MyGroup" 
                         Checked="RadioButton_Checked" />
            <RadioButton Name="NoRadioButton" 
                         Content="No" 
                         GroupName="MyGroup" 
                         Checked="RadioButton_Checked" />
            <TextBlock Name="RadioButtonTextBlock" />
        </StackPanel>

        <TextBlock Grid.Row="3" 
                   Text="ComboBox" 
                   Name="MyComboBox"  
                   VerticalAlignment="Center" />
        <StackPanel Orientation="Horizontal" 
                    Grid.Row="3" 
                    Grid.Column="1" 
                    Margin="20,10,0,10">
            <ComboBox SelectionChanged="ComboBox_SelectionChanged" >
                <ComboBoxItem Content="Fourth" />
                <ComboBoxItem Content="Fifth" />
                <ComboBoxItem Content="Sixth" IsSelected="True" />
            </ComboBox>
            <TextBlock Name="ComboBoxResultTextBlock" />
        </StackPanel>

        <TextBlock Grid.Row="4" Text="ListBox" VerticalAlignment="Center" />
        <StackPanel Grid.Row="4" Grid.Column="1"  Margin="20,10,0,10">
            <ListBox Name="MyListBox" 
                     SelectionMode="Multiple" 
                     SelectionChanged="ListBox_SelectionChanged">
                <ListBoxItem Content="First" />
                <ListBoxItem Content="Second" />
                <ListBoxItem Content="Third" />
            </ListBox>
            <TextBlock Name="ListBoxResultTextBlock" />
        </StackPanel>

        <TextBlock Grid.Row="5" Text="Image" VerticalAlignment="Center" />
        <Image Source="Assets/StoreLogo.png" 
               HorizontalAlignment="Left"
               Width="250"
               Height="50"
               Grid.Row="5" 
               Grid.Column="1" 
               Stretch="Uniform"
               Margin="20,10,0,10" />

        <TextBlock Grid.Row="7" Text="ToggleButton" VerticalAlignment="Center"  />
        <StackPanel Orientation="Horizontal" 
                    Grid.Row="7" 
                    Grid.Column="1"  
                    Margin="20,10,0,10" >
            <ToggleButton Name="MyToggleButton" 
                          Content="Premium Option" 
                          IsThreeState="True" 
                          Click="MyToggleButton_Click" />
            <TextBlock Name="ToggleButtonResultTextBlock" />
        </StackPanel>

        <TextBlock Grid.Row="8" 
                   Text="ToggleSwitch" 
                   VerticalAlignment="Center" />
        <StackPanel Grid.Row="8" 
                      Grid.Column="1"  
                      Margin="20,10,0,10" >
            <ToggleSwitch>
                <ToggleSwitch.OffContent>
                    <TextBlock Text="I'm off right now." />
                </ToggleSwitch.OffContent>
                <ToggleSwitch.OnContent>
                    <TextBlock Text="I'm on!" />
                </ToggleSwitch.OnContent>
            </ToggleSwitch>
        </StackPanel>



    </Grid>

cs 程式碼

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        private void MyCheckBox_Tapped(object sender, TappedRoutedEventArgs e)
        {
            CheckBoxResultTextBlock.Text = MyCheckBox.IsChecked.ToString();
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButtonTextBlock.Text = (bool)YesRadioButton.IsChecked ? "Yes" : "No";
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBoxResultTextBlock == null) return;

            var combo = (ComboBox)sender;
            var item = (ComboBoxItem)combo.SelectedItem;
            ComboBoxResultTextBlock.Text = item.Content.ToString();

        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedItems = MyListBox.Items.Cast<ListBoxItem>()
                                  .Where(p => p.IsSelected)
                                    .Select(t => t.Content.ToString())
                                      .ToArray();

            ListBoxResultTextBlock.Text = string.Join(", ", selectedItems);

        }

        private void MyToggleButton_Click(object sender, RoutedEventArgs e)
        {
            ToggleButtonResultTextBlock.Text = MyToggleButton.IsChecked.ToString();
        }
    }

相關推薦

UWP入門--簡單控制元件

每天看幾個,要不聊幾天我就可以看完啦,加油! 看效果 1. CheckBox <TextBlock Grid.Row="0" Text="CheckBox" Vertic

MFC入門-- 第一簡單的windows圖形化介面小程式開啟計算器,記事本,查IP

////////////////////////////////序////////////////////////////////   大約三年前,學過一些簡單的程式語言之後其實一直挺苦惱於所寫的程式總是拘泥於用的編譯器,脫離了編譯環境基本沒執行的可行性,故而寫一個在任意windows電腦下都能

Salesforce學習之路-admin篇簡單概念

Salesforce是一款非常強大的CRM(Customer Relationship Management)系統,國外企業使用十分頻繁,而國內目前僅有幾家在使用(當然,國內外企使用的依舊較多),因此相對來講,中文資料相對較少。這裡,結合之前參加的培訓和自己的一些理解,說說Salesforce的相關知識,如有

MFC入門-- MFC圖片/文字控制元件迴圈顯示文字和圖片的小程式

慣例附上前幾個部落格的連結: MFC入門(一)簡單配置:http://blog.csdn.net/zmdsjtu/article/details/52311107 MFC入門(二)讀取輸入字元:http://blog.csdn.net/zmdsjtu/article/details/52315088 &

[原創]FineUI祕密花園 — 下拉列表控制元件

下拉列表也是Web開發中常用的控制元件之一,用來從一組可選項中選取一項。FineUI中的下拉列表不僅可以通過ASPX標籤宣告,也可以繫結到各種資料來源。FineUI還根據實際專案的需要,擴充套件了一種模擬樹的下拉列表。 標籤宣告的下拉列表 1: <ext:Dr

Storm入門Storm實戰常見問題總結

logger 2.x appenders exec 問題 一個 新建 round XML 一、本地環境log級別設置問題 storm-core-1.1.0.jar下面有個log4j2.xml文件,默認log級別是info。 <configuration monitor

php獲取當前月份的前

return php 得到 date code pre for style str //獲取當前月份的前一月 function GetMonth($sign) { //得到系統的年月 $tmp_date=date("Ym"); //

10-Linux基礎入門-文件和目錄的屬性及權限之用戶與組和時間戳基礎

log 信息 p s center 新浪 shell 使用 自己的 用戶配置 一、概述Linux是一個多用戶、多任務的操作系統,對於Linux系統來說,由於角色不同,權限和所完成的任務也不同。用戶的角色是通過UID和GID識別的,用戶的UID就相當於我們的身份證一樣,用戶名

python入門捕獲異常及內置參數

內置時間參數 python3 捕獲異常的的語法 try: 運行代碼 except(名稱) 出現異常的運行代碼 else 沒有出現異常的運行代碼 raise 引發一個異常 finally 不論有沒有異常都運行 例子: try: 2/0 except Exception as e:(商量的語法)

區塊鏈快速入門——以太坊

hub rtg DApp 外部 試圖 技術 ransac 復雜 HERE 區塊鏈快速入門(八)——以太坊 一、以太坊簡介 以太坊(Ethereum)項目的最初目標是打造一個運行智能合約的平臺(Platform for Smart Contract),支持圖靈完備的應用,按照

cesium入門

cesium入門(八) 互動 我們可以通過三種方式來通過滑鼠和cesium互動 scene.pick 通過視窗座標來取一個物件 scene.drillPick取一個物件列表 Globe.pick 根據光線和地形取一個物件

Servlet入門ServletContext物件

前言       本章學習ServletContext物件的相關知識 方法 1.概念 我們知道,request物件解決的是同一請求下的資料共享問題,session解決了同一使用者(會話)下不同請求的資料共享問題,那麼不同使用者(會話)下的不同請求的資

java多執行緒快速入門

設定執行緒優先順序:join() package com.cppdy; class MyThreadA extends Thread{ MyThreadB b; public MyThreadA(MyThreadB b) { this.b=b; }

docker入門

如在文件中遇到什麼問題請聯絡作者 QQ:1172796094 本人正在找深圳Java實習工作,求大佬帶飛 —————————————————————————————————————— 實戰:自己構建redis映象 編寫Dockerfile如下: 構建Redis映象 itcast 基於Ce

HTTP入門:確認訪問使用者身份的認證

8.1何為認證 某些Web頁面只想讓特定的人瀏覽,或者乾脆僅本人可見。未達到這個目的,必不可少的就是認證功能。 服務端為了驗證客戶端登陸者的身份,該使用者是否真的具有訪問系統的許可權,就需要核對“登入者本人才知道的資訊”、“登入者本人才會有的資訊”。 核對的資

Java網路程式設計入門之網路協議

 網路協議          對於需要從事網路程式設計的程式設計師來說,網路協議是一個需要深刻理解的概念。那麼什麼是網路協議呢?          網路協議是指對於網路中傳輸的資料格式的規定。對於網路程式設計初學者來說,沒有必要深入瞭解TCP/IP協議簇,所以對於初學者來

遊戲開發入門遊戲中的場景管理

授課者通過2D圍棋的例子,逐步拓展成3D的虛擬遊戲世界。這個3D遊戲世界基本上就可以涵蓋市面上所有型別的遊戲了,我們通過各種手段去管理好這個遊戲世界,就能快速的進行各種遊戲邏輯的開發。 筆記與

java高併發概念

幾個概念 同步(synchronous)和非同步(asynchronous) (1)同步互動:指傳送一個請求,需要等待返回,然後才能夠傳送下一個請求,有個等待過程; (2)非同步互動:指傳送一個請求,不需要等待返回,隨時可以再發送下一個請求,即不需要等待。 區別

Flask Web開發入門之檔案上傳

本章我們介紹Flask Web開發中涉及的檔案上傳模組 定義後臺接收處理邏輯 # http://flask.pocoo.org/docs/0.12/patterns/fileuploads/ @app.route('/upload', methods=[

spring boot 入門filter、servlet、listener

spring boot 入門(八)servlet、filter、listener 1. filter spring boot有兩種方式來配置filter 1.1 Servlet 3.0新特性,以註解方式配置Filter,需在啟動類上加入@ServletComponentSc