自定义SL多选下拉框的问题

勇敢的心515 2011-05-13 05:03:08
我想自己做一个多选下拉框,样式如下:
<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}"></CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>

我在下拉框引用的时候是:
<ComboBox x:Name="chkTest" HorizontalAlignment="Left" Width="101">
<ComboBoxItem Style="{StaticResource comboxStyle1}" Content="AA"/>
<ComboBoxItem Style="{StaticResource comboxStyle1}" Content="BB"/>
</ComboBox>

当在多个下拉框选择了后,我想把下拉框的Contont在Combox上面显示出来,格式为: AA,BB 请问有什么好的思路?
谢谢!
...全文
59 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
勇敢的心515 2011-05-14
  • 打赏
  • 举报
回复
谢谢楼上了!
a184485789 2011-05-13
  • 打赏
  • 举报
回复

<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;
}

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧