UWP入门(八)--几个简单的控件
生活随笔
收集整理的這篇文章主要介紹了
UWP入门(八)--几个简单的控件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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入门(八)--几个简单的控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java日志框架Slf4j+Log4j入
- 下一篇: Day7 - 面向对象编程进阶及其他相关