24天学完C#第二篇
实验需求:
TextBox *1
RadioButton *6
ListBox *1
ComboBox *2
TextBlock *1
创建一个新的WPF项目

设置窗口标题

增加行和列

添加TextBox


增加C#代码来更新TextBlock
双击TextBox控件方法
(为TextBlock增加name)

(为changed编写逻辑)
private void numberTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
number.Text = numberTextBox.Text;
}
//这个版本存在bug,支持了非数值的显示
增加一个事件处理器只允许输入数字
(增加事件)

private void numberTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !int.TryParse(e.Text, out int result);
}
添加其余的XAML组件
(单选框按钮)

(向网格中间左边添加一个列表框)

(为列表框添加ListBoxItem)

(向中各中间右边的单元格增加两个不同的ComboBox)

(添加集合)

<Window x:Class="ExperimentWithCOntrols.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ExperimentWithCOntrols"
mc:Ignorable="d"
Title="ExperimentWIthControls" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<TextBox HorizontalAlignment="Left" FontSize="18" Text="Enter a number" VerticalAlignment="Top" Width="139" Height="26" Foreground="#FF3B2F2F" BorderBrush="White"/>
<TextBlock x:Name="number" Grid.Column="1" Grid.Row="0" TextWrapping="Wrap" Text="#" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" />
<TextBox x:Name="numberTextBox" FontSize ="18" TextWrapping="Wrap" Text="0" Margin="10,49,0,0" Width="120" HorizontalAlignment="Left" VerticalAlignment="Top" TextChanged="numberTextBox_TextChanged" PreviewTextInput="numberTextBox_PreviewTextInput"/>
<RadioButton Content="1" HorizontalAlignment="Left" Margin="230,127,0,0" VerticalAlignment="Top"/>
<RadioButton Content="2" HorizontalAlignment="Left" Margin="270,127,0,0" VerticalAlignment="Top"/>
<RadioButton Content="3" HorizontalAlignment="Left" Margin="311,127,0,0" VerticalAlignment="Top"/>
<RadioButton Content="4" HorizontalAlignment="Left" Margin="230,150,0,0" VerticalAlignment="Top"/>
<RadioButton Content="5" HorizontalAlignment="Left" Margin="270,150,0,0" VerticalAlignment="Top"/>
<RadioButton Content="6" HorizontalAlignment="Left" Margin="311,150,0,0" VerticalAlignment="Top"/>
<ListBox x:Name="myListBox" Grid.Row="1" Margin="10,10,10,10">
<ListBoxItem Content="1"/>
<ListBoxItem Content="2"/>
<ListBoxItem Content="3"/>
<ListBoxItem Content="4"/>
<ListBoxItem Content="5"/>
</ListBox>
<ComboBox x:Name="readOnlyComboBox" Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="120">
<ListBoxItem Content="1"/>
<ListBoxItem Content="2"/>
<ListBoxItem Content="3"/>
<ListBoxItem Content="4"/>
<ListBoxItem Content="5"/>
</ComboBox>
<ComboBox x:Name="editableComboBox" Grid.Column="1" HorizontalAlignment="Left" Margin="270,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="120" IsEditable="True">
<ListBoxItem Content="1"/>
<ListBoxItem Content="2"/>
<ListBoxItem Content="3"/>
<ListBoxItem Content="4"/>
<ListBoxItem Content="5"/>
</ComboBox>
</Grid>
</Window>
为网格最下面一行增加滑动条
(添加小滑动条)

(双击小滑动条为其添加事件)
private void smallSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
number.Text = smallSlider.Value.ToString("0");
}
(增加一个滑动条来选择电话号码)

(双击滑动条添加valuechange事件)
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Slider t = sender as Slider;//也可以直接指定bigSlider
if (t != null)
{
number.Text = t.Value.ToString("000-000-00000");
}
}
增加C#代码让其余控件开始工作
(RadioButton控制number.text)
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
RadioButton r = sender as RadioButton;
if (r != null) {
number.Text = r.Content.ToString();
}
}
(ListBox更新TextBlock)
private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
{
if (myListBox.SelectedItem is ListBoxItem boxItem) {
number.Text = boxItem.Content.ToString();
}
}
(只读组合框更新TextBlock)
private void readOnlyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (readOnlyComboBox.SelectedItem is ListBoxItem item) {
number.Text = item.Content.ToString();
}
}
(可编辑组合框更新TextBlock)

private void editableComboBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender is ComboBox combo) {
number.Text = combo.Text;
}
}
总结
(XAML代码)
<Window x:Class="ExperimentWithCOntrols.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ExperimentWithCOntrols"
mc:Ignorable="d"
Title="ExperimentWIthControls" Height="450" Width="800">
<Grid x:Name="bigSlider">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<TextBox HorizontalAlignment="Left" FontSize="18" Text="Enter a number" VerticalAlignment="Top" Width="139" Height="26" Foreground="#FF3B2F2F" BorderBrush="White"/>
<TextBlock x:Name="number" Grid.Column="1" Grid.Row="0" TextWrapping="Wrap" Text="#" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" />
<TextBox x:Name="numberTextBox" FontSize ="18" TextWrapping="Wrap" Text="0" Margin="10,49,0,0" Width="120" HorizontalAlignment="Left" VerticalAlignment="Top" TextChanged="numberTextBox_TextChanged" PreviewTextInput="numberTextBox_PreviewTextInput"/>
<RadioButton Content="1" HorizontalAlignment="Left" Margin="230,127,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
<RadioButton Content="2" HorizontalAlignment="Left" Margin="270,127,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
<RadioButton Content="3" HorizontalAlignment="Left" Margin="311,127,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
<RadioButton Content="4" HorizontalAlignment="Left" Margin="230,150,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
<RadioButton Content="5" HorizontalAlignment="Left" Margin="270,150,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
<RadioButton Content="6" HorizontalAlignment="Left" Margin="311,150,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/>
<ListBox x:Name="myListBox" Grid.Row="1" Margin="10,10,10,10">
<ListBoxItem Content="1" Selected="ListBoxItem_Selected"/>
<ListBoxItem Content="2"/>
<ListBoxItem Content="3"/>
<ListBoxItem Content="4"/>
<ListBoxItem Content="5"/>
</ListBox>
<ComboBox x:Name="readOnlyComboBox" Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="120" SelectionChanged="readOnlyComboBox_SelectionChanged">
<ListBoxItem Content="1"/>
<ListBoxItem Content="2"/>
<ListBoxItem Content="3"/>
<ListBoxItem Content="4"/>
<ListBoxItem Content="5"/>
</ComboBox>
<ComboBox x:Name="editableComboBox" Grid.Column="1" HorizontalAlignment="Left" Margin="270,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="120" IsEditable="True" TextBoxBase.TextChanged="editableComboBox_TextChanged">
<ListBoxItem Content="1"/>
<ListBoxItem Content="2"/>
<ListBoxItem Content="3"/>
<ListBoxItem Content="4"/>
<ListBoxItem Content="5"/>
</ComboBox>
<Slider x:Name="smallSlider" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="2" VerticalAlignment="Top" Width="120" AutoToolTipPlacement="TopLeft" Maximum="5" Minimum="1" ValueChanged="smallSlider_ValueChanged"/>
<Slider Margin="10,0,142,0" Grid.Row="2" VerticalAlignment="Center" Grid.ColumnSpan="2" Minimum="11111111111" Maximum="99999999999" ValueChanged="Slider_ValueChanged"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ExperimentWithCOntrols
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void numberTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
number.Text = numberTextBox.Text;
}
private void numberTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !int.TryParse(e.Text, out int result);
}
private void smallSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
number.Text = smallSlider.Value.ToString("0");
}
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Slider t = sender as Slider;//也可以直接指定bigSlider
if (t != null)
{
number.Text = t.Value.ToString("000-000-00000");
}
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
RadioButton r = sender as RadioButton;
if (r != null) {
number.Text = r.Content.ToString();
}
}
private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
{
if (myListBox.SelectedItem is ListBoxItem boxItem) {
number.Text = boxItem.Content.ToString();
}
}
private void readOnlyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (readOnlyComboBox.SelectedItem is ListBoxItem item) {
number.Text = item.Content.ToString();
}
}
private void editableComboBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender is ComboBox combo) {
number.Text = combo.Text;
}
}
}
}