背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

2021-05-12 22:26

阅读:529

标签:http   ext   extend   multi   type   resource   数据   block   mode   

原文:背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

[源码下载]


背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

作者:webabcd


介绍
背水一战 Windows 10 之 控件(选择类)

  • ListBox
  • RadioButton
  • CheckBox
  • ToggleSwitch

示例
1、ListBox 的示例
Controls/SelectionControl/ListBoxDemo.xaml

Page
    x:Class="Windows10.Controls.SelectionControl.ListBoxDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.SelectionControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    
    xmlns:common="using:Windows10.Common">

    Grid Background="Transparent">
        StackPanel Margin="10 0 10 10" Orientation="Horizontal">

            

            
            ListBox x:Name="listBox1" Margin="5" Width="200" Height="300" HorizontalAlignment="Left" VerticalAlignment="Top">
                ListBox.Items>
                    ListBoxItem Content="ListBoxItem1" />
                    ListBoxItem Content="ListBoxItem2" />
                    ListBoxItem Content="ListBoxItem3" />
                    ListBoxItem Content="ListBoxItem4" />
                    ListBoxItem Content="ListBoxItem5" />
                ListBox.Items>
            ListBox>

            ListBox x:Name="listBox2" Margin="5" ItemsSource="{x:Bind Employees}" Width="200" Height="300" HorizontalAlignment="Left" VerticalAlignment="Top">
                ListBox.ItemTemplate>
                    DataTemplate x:DataType="common:Employee">
                        StackPanel Orientation="Horizontal">
                            TextBlock Text="{x:Bind Name}" />
                            TextBlock Text="{x:Bind Age}" Margin="5 0 0 0" />
                        StackPanel>
                    DataTemplate>
                ListBox.ItemTemplate>
            ListBox>
            TextBlock Name="lblMsg2" Margin="5" Width="300" TextWrapping="Wrap" />

        StackPanel>
    Grid>
Page>

Controls/SelectionControl/ListBoxDemo.xaml.cs

/*
 * ListBox - 列表框控件(继承自 Selector, 请参见 /Controls/SelectionControl/SelectorDemo.xaml)
 *     SelectionMode - 选择的模式
 *         Single - 单选(默认)
 *         Multiple - 仅通过鼠标多选
 *         Extended - 通过鼠标和辅助键(ctrl, shift)多选
 *     ScrollIntoView(object item) - 滚动到指定数据对象
 *     SelectAll() - 选中所有项
 *     SelectedItems - 获取当前选中的数据对象集合
 *     
 *     
 * ListBoxItem - 列表框控件的 item(继承自 SelectorItem, 请参见 /Controls/SelectionControl/SelectorDemo.xaml)
 */

using System.Collections.ObjectModel;
using Windows.UI.Xaml.Controls;
using Windows10.Common;
using System.Linq;

namespace Windows10.Controls.SelectionControl
{
    public sealed partial class ListBoxDemo : Page
    {
        public ObservableCollection Employees { get; set; } = TestData.GetEmployees(30);

        public ListBoxDemo()
        {
            this.InitializeComponent();

            // 通过鼠标结合 ctrl键 shift键 多选
            listBox1.SelectionMode = SelectionMode.Extended;

            // 仅通过鼠标多选
            listBox2.SelectionMode = SelectionMode.Multiple;
            listBox2.Loaded += ListBox2_Loaded;
        }

        private void ListBox2_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            listBox2.SelectAll();
            // 滚动到最后一条数据
            listBox2.ScrollIntoView(this.Employees.Last());

            lblMsg2.Text = string.Join(", ", listBox2.SelectedItems.Cast().Select(p => p.Name));
        }
    }
}


2、RadioButton 的示例
Controls/SelectionControl/RadioButtonDemo.xaml

Page
    x:Class="Windows10.Controls.SelectionControl.RadioButtonDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.SelectionControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    Grid Background="Transparent">
        StackPanel Margin="10 0 10 10">
            
            

            RadioButton Name="rad1" GroupName="groupName1" Margin="5" IsChecked="True" Content="groupName1 - RadioButton1" />
            RadioButton Name="rad2" GroupName="groupName1" Margin="5" Content="groupName1 - RadioButton2" />

            RadioButton Name="rad3" GroupName="groupName2" Margin="5" IsChecked="True" Content="groupName2 - RadioButton1" />
            RadioButton Name="rad4" GroupName="groupName2" Margin="5" Content="groupName2 - RadioButton2" />

        StackPanel>
    Grid>
Page>

Controls/SelectionControl/RadioButtonDemo.xaml.cs

/*
 * RadioButton - 单选框控件(继承自 ToggleButton, 请参见 /Controls/ButtonControl/ToggleButtonDemo.xaml)
 */

using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.SelectionControl
{
    public sealed partial class RadioButtonDemo : Page
    {
        public RadioButtonDemo()
        {
            this.InitializeComponent();
        }
    }
}


3、CheckBox 的示例
Controls/SelectionControl/CheckBoxDemo.xaml

Page
    x:Class="Windows10.Controls.SelectionControl.CheckBoxDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.SelectionControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    Grid Background="Transparent">
        StackPanel Margin="10 0 10 10">

            

            CheckBox Name="chk1" Margin="5" IsChecked="True" Content="CheckBox1" />

            CheckBox Name="chk2" Margin="5" IsChecked="False" Content="CheckBox2" />

        StackPanel>
    Grid>
Page>

Controls/SelectionControl/CheckBoxDemo.xaml.cs

/*
 * CheckBox - 复选框控件(继承自 ToggleButton, 请参见 /Controls/ButtonControl/ToggleButtonDemo.xaml)
 */

using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.SelectionControl
{
    public sealed partial class CheckBoxDemo : Page
    {
        public CheckBoxDemo()
        {
            this.InitializeComponent();
        }
    }
}


4、ToggleSwitch 的示例
Controls/SelectionControl/ToggleSwitchDemo.xaml

Page
    x:Class="Windows10.Controls.SelectionControl.ToggleSwitchDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.SelectionControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    Grid Background="Transparent">
        
        StackPanel Margin="10 0 10 10">

            TextBlock Name="lblMsg" Margin="5" />
            
            
            
            ToggleSwitch Name="toggleSwitch1" Margin="5" Header="wifi1" OnContent="OnContent" OffContent="OffContent" IsOn="True"
                          Toggled="toggleSwitch1_Toggled" 
                          Style="{StaticResource MyToggleSwitchStyle}" />

            ToggleSwitch Name="toggleSwitch2" Margin="5" IsOn="True" Style="{StaticResource MyToggleSwitchStyle}">
                ToggleSwitch.HeaderTemplate>
                    DataTemplate>
                        TextBlock Text="wifi2" Foreground="Yellow" />
                    DataTemplate>
                ToggleSwitch.HeaderTemplate>
                ToggleSwitch.OnContentTemplate>
                    DataTemplate>
                        TextBlock Text="OnContent" Foreground="Orange" />
                    DataTemplate>
                ToggleSwitch.OnContentTemplate>
                ToggleSwitch.OffContentTemplate>
                    DataTemplate>
                        TextBlock Text="OffContent" Foreground="Blue" />
                    DataTemplate>
                ToggleSwitch.OffContentTemplate>
            ToggleSwitch>

        StackPanel>
    Grid>
Page>

Controls/SelectionControl/ToggleSwitchDemo.xaml.cs

/*
 * ToggleSwitch - 状态切换控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
 */

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.SelectionControl
{
    public sealed partial class ToggleSwitchDemo : Page
    {
        public ToggleSwitchDemo()
        {
            this.InitializeComponent();
        }

        private void toggleSwitch1_Toggled(object sender, RoutedEventArgs e)
        {
            lblMsg.Text = $"toggleSwitch1_Toggled, IsOn:{toggleSwitch1.IsOn}";
        }
    }
}

OK
[源码下载]

背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

标签:http   ext   extend   multi   type   resource   数据   block   mode   

原文地址:http://www.cnblogs.com/lonelyxmas/p/7567313.html


评论


亲,登录后才可以留言!