我的群组有问题。在组框中我有一个网格,我有大约20个复选框。 问题是我需要将上面所有选中的复选框的值显示到另一个组框中。
我在WPF C#中工作。
触发上述事件的事件是什么?
如果未选中选中的复选框,则第二组框中显示的值将消失。
答案 0 :(得分:1)
在Wpf中,您有Checked Event,UnChecked Event和Indeterminate Event他们都使用通用RoutedEventHandler
,因此您应该可以使用公共事件处理程序连接它们,检查Tag
媒体资源中的发件人姓名或唯一身份证明,并使用该身份更改另一个GroupBox
中的值。
<强>的Xaml 强>
<CheckBox Content="CheckBox" Checked="checkedChanged" Unchecked="checkedChanged" Indeterminate="checkedChanged" Height="16" HorizontalAlignment="Left" Margin="10,10,0,0" Name="checkBox1" VerticalAlignment="Top" Tag="1" />
<CheckBox Content="CheckBox" Checked="checkedChanged" Unchecked="checkedChanged" Indeterminate="checkedChanged" Height="16" HorizontalAlignment="Left" Margin="10,36,0,0" Name="checkBox2" VerticalAlignment="Top" Tag="2" />
<强> Xaml.cs 强>
private void checkedChanged(object sender, RoutedEventArgs e)
{
CheckBox cb = (CheckBox)sender;
int index;
if (int.TryParse(cb.Tag.ToString(), out index))
{
if (cb.IsChecked == true)
{
switch (index)
{
case 1:
break;
case 2:
break;
default:
break;
}
}
else if (cb.IsChecked == false)
{
switch (index)
{
case 1:
break;
case 2:
break;
default:
break;
}
}
else
{
switch (index)
{
case 1:
break;
case 2:
break;
default:
break;
}
}
}
}
我在评论中提到的例子:
<强>的Xaml 强>
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<GroupBox Grid.Column="0" BorderBrush="Black" BorderThickness="2">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CheckBox Grid.Column="0" Grid.Row="0" Content="CheckBox" Checked="checkedChanged" Unchecked="checkedChanged" Indeterminate="checkedChanged" Height="16" HorizontalAlignment="Stretch" Name="checkBox1" VerticalAlignment="Top" Tag="0" />
<CheckBox Grid.Column="1" Grid.Row="0" Content="CheckBox" Checked="checkedChanged" Unchecked="checkedChanged" Indeterminate="checkedChanged" Height="16" HorizontalAlignment="Stretch" Name="checkBox2" VerticalAlignment="Top" Tag="1" />
<CheckBox Grid.Column="0" Grid.Row="1" Content="CheckBox" Checked="checkedChanged" Unchecked="checkedChanged" Indeterminate="checkedChanged" Height="16" HorizontalAlignment="Stretch" Name="checkBox3" VerticalAlignment="Top" Tag="2" />
<CheckBox Grid.Column="1" Grid.Row="1" Content="CheckBox" Checked="checkedChanged" Unchecked="checkedChanged" Indeterminate="checkedChanged" Height="16" HorizontalAlignment="Stretch" Name="checkBox4" VerticalAlignment="Top" Tag="3" />
</Grid>
</GroupBox>
<GroupBox Grid.Column="1" BorderBrush="Black" BorderThickness="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Content="" HorizontalAlignment="Stretch" Name="label1" VerticalAlignment="Top" Tag="1" />
<Label Grid.Column="1" Grid.Row="0" Content="" HorizontalAlignment="Stretch" Name="label2" VerticalAlignment="Top" Tag="2" />
<Label Grid.Column="0" Grid.Row="1" Content="" HorizontalAlignment="Stretch" Name="label3" VerticalAlignment="Top" Tag="3" />
<Label Grid.Column="1" Grid.Row="1" Content="" HorizontalAlignment="Stretch" Name="label4" VerticalAlignment="Top" Tag="4" />
</Grid>
</GroupBox>
</Grid>
</Window>
<强> Xaml.cs 强>
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Label[] myLabels = new Label[4];
public MainWindow()
{
InitializeComponent();
myLabels[0]=label1;
myLabels[1]=label2;
myLabels[2]=label3;
myLabels[3]=label4;
}
private void checkedChanged(object sender, RoutedEventArgs e)
{
CheckBox cb = (CheckBox)sender;
int index;
if (int.TryParse(cb.Tag.ToString(), out index))
{
if (cb.IsChecked == true)
{
myLabels[index].Content="Checked";
}
else if (cb.IsChecked == false)
{
myLabels[index].Content="UnChecked";
}
else
{
myLabels[index].Content="?";
}
}
}
}
}