多說無益,,代碼兩部分如下所示:
代碼如下:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="160*"/> <ColumnDefinition Width="10"/> <ColumnDefinition Width="250*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="70"/> <RowDefinition Height="70"/> <RowDefinition Height="70"/> <RowDefinition Height="70"/> <RowDefinition Height="40"/> <RowDefinition Height="120"/> <RowDefinition Height="70"/> <RowDefinition Height="70"/> </Grid.RowDefinitions> <TextBlock Name="textBlock0" Text="姓名:" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="0" /> <TextBlock Name="textBlock1" Text="工作:" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="1" /> <TextBlock Name="textBlock2" Text="哈哈你懂得:" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="4" /> <TextBlock Name="textBlock3" Text="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="5" /> <TextBox Name="textBox_Nam2e" Text="" Grid.Column="2" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> <CheckBox Name="checkBox1" Content="" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> <RadioButton Name="radioButton1" GroupName="myGroup" Content="FBI" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> <RadioButton Name="radioButton2" GroupName="myGroup" Content="CIA" Grid.Column="2" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> <ListBox Name="myListBox" Grid.Column="0" Grid.Row="5" Grid.ColumnSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > <ListBoxItem Name="listBoxItem0" Content="奧特曼" /> <ListBoxItem Name="listBoxItem1" Content="孫悟空" /> <ListBoxItem Name="listBoxItem2" Content="關羽" /> <ListBoxItem Name="listBoxItem3" Content="周星星" /> </ListBox> <Button Name="button" Content="Save" Grid.Column="3" Grid.Row="6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /></Grid>
以上控制項的後台操作:
if (checkBox1.IsChecked==true) { MessageBox.Show("this.checkBox1.IsChecked"); } if (radioButton1.IsChecked == true) { MessageBox.Show("FBI"); } foreach (ListBoxItem myList in myListBox.Items) { if (myList.IsSelected) { MessageBox.Show(myList.Name+"||"+myList.Content); } }
P.S:兩個按鈕共用一個點擊事件
相似的事件操作可以採用該方法。
private void button_Click(object sender, RoutedEventArgs e){ Button myButton=(Button)sender; textBlock.Text=myButton.Name; }