UWP入门(八)--几个简单的控件

简介: 原文:UWP入门(八)--几个简单的控件 每天看几个,要不聊几天我就可以看完啦,加油! 看效果 1. CheckBox pr...
原文: 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();
        }
    }
目录
相关文章
|
C# Windows
2000条你应知的WPF小姿势 基础篇<74-77 WPF 多窗口Tips>
2000条你应知的WPF小姿势 基础篇<74-77 WPF 多窗口Tips>
120 0
|
C# Windows
2000条你应知的WPF小姿势 基础篇<78-81 Dialog/Location/WPF设备无关性>
2000条你应知的WPF小姿势 基础篇<78-81 Dialog/Location/WPF设备无关性>
95 0
|
C# Windows
wpf怎么使用WindowsFormsHost(即winform控件)
原文:wpf怎么使用WindowsFormsHost(即winform控件) 使用方法:   1、首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.
5489 0
|
Java C# 索引
C#之 十九 使用WinForm控件
C#之 十九 使用WinForm控件
230 0
|
Web App开发
艾伟:WinForm控件开发总结(三)------认识WinForm控件常用的Attribute
在前面的文章里我们制作了一个非常简单的控件。现在我们回过头来看看这些代码透露出什么信息。   这个类是直接从Control类派生出来的,自定义控件都是直接从Control类派生出来的。这个类定义了一个属性TextAlignment,用来控制文本在控件中显示的位置:           ...
1020 0
|
测试技术
艾伟:WinForm控件开发总结(二)------使用和调试自定义控件
在上一篇文章里我们创建了一个简单的控件FirstControl,现在我来介绍一下怎么使用和调试自己的控件。我希望将过程写的尽可能的详细,让想学习控件开发的朋友容易上手,高手们见谅。       在同一个solution里添加一个Windows Application工程(在Solution Explorer里右键点击CustomControlSample solution选择Add->New Project…),命名为TestControl。
898 0