WPF 获取ListBox子控件

sloveb520 2011-11-14 04:31:56
<ListBox Height="166" HorizontalAlignment="Left" Margin="109,57,0,0" Name="listBox1" VerticalAlignment="Top" Width="279">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="cbNumber" Content="{Binding Title}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

如何选中listBox某个CheckBox,然后回车,就选中CheckBox,求解
...全文
322 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sloveb520 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 studyregister 的回复:]
加一属性:IsSynchronizedWithCurrentItem="True"
后台:
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.listBox1.SelectedItem != null)
{
ListBoxItem……
[/Quote]

哦 可以了谢谢 没添加那个属性
sloveb520 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 studyregister 的回复:]
加一属性:IsSynchronizedWithCurrentItem="True"
后台:
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.listBox1.SelectedItem != null)
{
ListBoxItem……
[/Quote]

如果我多添加几个checkBox,他只能获取第一个,找不到后面的,这是什么原因啊
Sundays 2011-11-14
  • 打赏
  • 举报
回复 2
加一属性:IsSynchronizedWithCurrentItem="True"
后台:
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.listBox1.SelectedItem != null)
{
ListBoxItem myListBoxItem = (ListBoxItem)(listBox1.ItemContainerGenerator.ContainerFromItem(listBox1.Items.CurrentItem));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
CheckBox tempCheckBox = (CheckBox)myDataTemplate.FindName("cbNumber", myContentPresenter);

if (tempCheckBox != null)
{
tempCheckBox.IsChecked = true;
}
}
}
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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