8,735
社区成员
发帖
与我相关
我的任务
分享
<UserControl.Resources>
<Style x:Key="comboxStyle1" TargetType="ComboBoxItem">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<CheckBox Content="{TemplateBinding Content}" Click="CheckBox_Click"></CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ComboBox x:Name="chkTest" HorizontalAlignment="Left" Width="101" Height="50" DropDownOpened="chkTest_DropDownOpened" DropDownClosed="chkTest_DropDownClosed">
<ComboBoxItem Style="{StaticResource comboxStyle1}" Content="AA"/>
<ComboBoxItem Style="{StaticResource comboxStyle1}" Content="BB"/>
<ComboBoxItem></ComboBoxItem>
</ComboBox>
</Grid>
List<CheckBox> CheckList = new List<CheckBox>();
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
if ((sender as CheckBox).IsChecked == true)
{
CheckList.Add(sender as CheckBox);
}
else
{
CheckList.Remove(sender as CheckBox);
}
}
private void chkTest_DropDownOpened(object sender, EventArgs e)
{
chkTest.Items.RemoveAt(chkTest.Items.Count - 1);
}
private void chkTest_DropDownClosed(object sender, EventArgs e)
{
string name = "";
foreach (var one in CheckList)
{
name += one.Content + ",";
}
chkTest.Items.Add(new ComboBoxItem() { Content = name.ToString() });
chkTest.SelectedIndex = chkTest.Items.Count - 1;
}