wpf中,xaml界面上有多个checkbox,还有一个listbox,我选中一个checkbox旁边的listbox就会立刻显示我所选checkbox的content内容。请问各位高手如何实现?谢谢!!
页面代码: <CheckBox Content="CheckBox111" Height="16" HorizontalAlignment="Left" Margin="30,38,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked" />
<CheckBox Content="CheckBox22" Height="16" HorizontalAlignment="Left" Margin="30,76,0,0" Name="checkBox2" VerticalAlignment="Top" Click="checkBox1_Checked" />
<CheckBox Content="CheckBox33" Height="16" HorizontalAlignment="Left" Margin="30,126,0,0" Name="checkBox3" VerticalAlignment="Top" Click="checkBox1_Checked" />
<ListBox Height="117" HorizontalAlignment="Left" Margin="168,29,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
后台代码:
private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
CheckBox chk = (CheckBox)sender;
if (chk.IsChecked == true)
{
listBox1.Items.Add(chk.Content);
}
}
如果不想添加重复的,就可以这样写:
private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
CheckBox chk = (CheckBox)sender;
if (chk.IsChecked == true)
{
if (!listBox1.Items.Contains(chk.Content))
{
listBox1.Items.Add(chk.Content);
}
}
}
谢谢您!
再问一个问题啊,那如果checkbox是通过后台代码加入的那怎么实现上面的功能呢?
@答应不爱你: 页面代码:
<Grid Loaded="Grid_Loaded">
<StackPanel Name="stac" Width="100px" Height="200px" Margin="12,12,390,98">
</StackPanel>
<ListBox Height="117" HorizontalAlignment="Left" Margin="168,29,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>
后台代码:
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
CheckBox c = new CheckBox();
c.Height = 20;
c.Width = 30;
c.Content = "1111";
c.IsChecked = false;
this.stac.Children.Add(c);
c.Click += new RoutedEventHandler(c_Click);
CheckBox c1 = new CheckBox();
c.Height = 20;
c.Width = 30;
c1.Content = "2222222";
c1.IsChecked = false;
this.stac.Children.Add(c1);
c1.Click += new RoutedEventHandler(c_Click);
}
void c_Click(object sender, RoutedEventArgs e)
{
CheckBox chk = (CheckBox)sender;
if (chk.IsChecked == true)
{
if (!listBox1.Items.Contains(chk.Content))
{
listBox1.Items.Add(chk.Content);
}
}
}
样式很难看,我没有调,你自己调一下吧。
@PursueZhang: 我想再问您另外一个问题。就是在解决方案资源管理器中我定义了一个文件夹Controls,里面有十个usercontrol,每个功能都不一样。在一个界面上将要调用这些用户控件。xaml中我加入了xmlns:local="clr-namespace:Student.UserControls",在控件grid中我加入了
<StackPanel Height="289" HorizontalAlignment="Left" Margin="439,0,0,216" Name="stackPanel1" VerticalAlignment="Bottom" Width="457" >
<UserControl x:Name="checkReport">
<local:report></local:report>
</UserControl>
</StackPanel>
在后台代码怎样修改粗体字部分,进而更改使用不同的用户控件呢?
@答应不爱你: 这个我没有做过,应该是用户控件中改字体。使用不同的用户控件是根据你的判断来显示或者隐藏用户控件。
@PursueZhang: 哦,谢谢啊!
@答应不爱你:
@答应不爱你: