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

posted on 2017-09-20 13:16 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/7560937.html

UWP入门(八)--几个简单的控件相关推荐

  1. android 标签样式布局,安卓 Tab 标签-利用 TabHost、TabWidget、FrameLayout 啰里八嗦实现 Tab 标签控件...

    安卓 Tab 标签-利用 TabHost.TabWidget.FrameLayout 啰里八嗦实现 Tab 标签控件 首先 XML 布局代码 TabHost 是整个 Tab 的外围,TabWidget ...

  2. (八)ASP.NET自定义用户控件(1)

    http://blog.csdn.net/laodao1/article/details/5897366 ASP.NET自定义控件组件开发 第一章:从一个简单的控件谈起 起始开发ASP.NET自定义控 ...

  3. 使用ATL创建简单ActiveX控件(一) —— 创建ATL项目

    创建过程以VS2010为例,分三篇(创建ATL项目.添加方法/属性和枚举.添加连接点)演示.本篇演示创建ATL项目. 传送门: <使用ATL创建简单ActiveX控件(二) -- 添加方法/属性 ...

  4. UWP WP8.1 依赖属性和用户控件 依赖属性简单使用 uwp添加UserControl

    上面说 附加属性.这章节说依赖属性. 所谓依赖属性.白话讲就是添加一个公开的属性. 同样,依赖属性的用法和附加属性的用法差不多. 依赖属性是具有一个get,set的属性,以及反调函数. 首先是声明依赖 ...

  5. VS2010/MFC编程入门之二十三(常用控件:按钮控件的编程实例)

    上一节VS2010/MFC编程入门教程中鸡啄米讲了按钮控件Button.Radio Button和Check Box的基本用法,本节就继续讲按钮控件的内容,通过一个实例让大家更清楚按钮控件在实际的软件 ...

  6. VS2010/MFC编程入门之二十(常用控件:静态文本框)

    上一节鸡啄米讲了颜色对话框之后,关于对话框的使用和各种通用对话框的介绍就到此为止了.从本节开始鸡啄米将讲解各种常用控件的用法.常用控件主要包括:静态文本框.编辑框.单选按钮.复选框.分组框.列表框.组 ...

  7. Android 第十八课 强大的滚动控件 RecyclerView

    步骤: 一.添加依赖库 compile'com.android.support:recyclerview-v7:26.1.0' 二.在activity_mian.xml中,添加RecyclerView ...

  8. 自定义控件 一 创建最简单的控件

    本来很早就应该写这篇文章了,但由于个人电脑坏了几个月了,这段时间又发生了很多事情,所以就耽搁了. 先开发一个最简单的Qt自定义控件,这个自定义的控件可以在QtCreator中拖放使用. 1.新建一个Q ...

  9. Asp.Net Web控件 (八)(TabControl 选项卡控件)

    在项目开发中经常会用到选项卡控件,网上也有很多,其实只是简单的功能,很多却实现的很复杂,功能很强大,并不是我需要的. 下面来实现一个简单的TabControl . 先看演示: 位置:TabContor ...

最新文章

  1. AAAI 2020 | MaskGEC:通过动态掩蔽改善语法纠错
  2. windows中遍历指定文件夹下的所有子文件夹
  3. JAVA中int、String的类型转换(亲测)
  4. 动效设计中的隐喻-2
  5. Always keep in mind
  6. Linux系统编程(一)
  7. jeecms添加站点
  8. Linux搭建高并发高可用Redis集群
  9. RAC 安装完成后 节点间通信不依赖于SSH
  10. 超级计算机操作系统有什么不同,超级计算机功能强大吗?它与普通计算机不同,但也使用Windows系统...
  11. mysql5.6 多实例 主从安装_MySQL5.6一主多从的半同步复制实例
  12. 前后端分离导出excel_Vue + .NetCore前后端分离的快速发开框架
  13. 利用EXP/IMP进行数据迁移,如何转换表空间操作(完整版)
  14. 基于java的小型超市管理系统系统(含源文件)
  15. 申请百度云文字识别OCR
  16. this的五种绑定方式
  17. 小型的 JavaScript 虚拟键盘
  18. python交互式怎么保存_如何保存一个Python交互式会话?
  19. siblings的用法
  20. php mysql 占位符_PDO中预处理语句占位符的使用

热门文章

  1. animateWithDuration:animations:completion:
  2. 【数据平台】关于Hadoop集群namenode format安全事故
  3. 【Python学习系列二十三】Scikit_Learn库降维方法(矩阵分解)-PCAFA
  4. 【Python学习系列三】Windows下Python第三方常用库安装
  5. vc操作windows服务(services.msc)
  6. Leetcode 211. 添加与搜索单词 - 数据结构设计 解题思路及C++实现
  7. 箭头函数中的this的使用
  8. JDK源码解析 迭代器模式在JAVA的很多集合类中被广泛应用,接下来看看JAVA源码中是如何使用迭代器模式的。
  9. Python 技术篇-不使用os模块遍历文件夹,pathlib库获取直接下级文件和所有下级文件
  10. JavaScript 技术篇-JSON字符串在线快速格式化查看实例演示,json.cn网址格式化json字符串