我的组框有问题。在组框中,我有一个网格,其中有大约 20 个复选框。问题是我需要将上面所有选中复选框的值显示到另一个组框中。
我在 WPF C# 中工作。
触发上述事件的事件是什么?
如果未选中选中的复选框,则第二组框中显示的值应该消失。
在 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="?";
}
}
}
}
}