一、不可空数据类型 int DateTime boolean
string str1 = ""; string s1 = null; MessageBox.Show(str1.Length.ToString()); //MessageBox.Show(s1.Length.ToString()); //空指针会引发异常 int? i1 = null; int i2 = 6; i1 = i2; int? i3 = null; //error //int i4 = i3; int i4 = (int)i3; //ok,但是如果传入的是NULL类型,会引发异常
二、常用控件
1、PasswordBox 属性password,设置默认密码
<PasswordBox Password="123" Height="23" HorizontalAlignment="Left" Margin="88,39,0,0" Name="psw" VerticalAlignment="Top" Width="120" />
2、checkbox 属性IsChecked 默认返回boolean? 可空数据类型,因此可以用 == true方法解决是比较好的
private void checkBox1_Checked(object sender, RoutedEventArgs e) { if (checkBox1.IsChecked == true) { MessageBox.Show("确认过眼神"); } }
3、RadioButton GroupName属性 组的名称
<RadioButton GroupName="Name" Content="RadioButton" Height="16" HorizontalAlignment="Left" Margin="111,122,0,0" Name="radioButton1" VerticalAlignment="Top" /> <RadioButton GroupName="Name" Content="RadioButton" Height="16" HorizontalAlignment="Left" Margin="111,144,0,0" Name="radioButton2" VerticalAlignment="Top" /> <RadioButton GroupName="Name" Content="RadioButton" Height="16" HorizontalAlignment="Left" Margin="111,166,0,0" Name="radioButton3" VerticalAlignment="Top" /> <RadioButton GroupName="age" Content="RadioButton" Height="16" HorizontalAlignment="Left" Margin="269,131,0,0" Name="radioButton4" VerticalAlignment="Top" /> <RadioButton GroupName="age" Content="RadioButton" Height="16" HorizontalAlignment="Left" Margin="269,153,0,0" Name="radioButton5" VerticalAlignment="Top" />
4、datePicker 控件 SelectedDate 赋予时间,获取时间
private void button1_Click(object sender, RoutedEventArgs e) { // 取值 DateTime? value = datePicker1.SelectedDate; if (value == null) { MessageBox.Show("请选择日期"); } else { MessageBox.Show(value.ToString()); } } private void button2_Click(object sender, RoutedEventArgs e) { datePicker1.SelectedDate = DateTime.Today; MessageBox.Show(DateTime.Today.ToString()); // today 属性只包含年月日 MessageBox.Show(DateTime.Now.ToString()); }
5、ProgressBar 控件 属性Maximum、Minimum、Value、IsIndeterminate(用于不可控因素)
<ProgressBar Maximum="100" Minimum="0" Value="10" Height="10" HorizontalAlignment="Left" Margin="24,197,0,0" Name="progressBar1" VerticalAlignment="Top" Width="100" /> <ProgressBar IsIndeterminate="True" Height="10" HorizontalAlignment="Left" Margin="174,197,0,0" Name="progressBar2" VerticalAlignment="Top" Width="100" /> <Button Content="加血" Height="23" HorizontalAlignment="Left" Margin="24,213,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" />
private void button3_Click(object sender, RoutedEventArgs e) { progressBar1.Value += 5; }