无法到达列表框项目中的复选框或将数据绑定到它,或复选框到文本块
Failing to reach checkbox within listbox item or bind data to it, or the checkbox to the textblock
我的项目 class 已选择字符串名称和布尔值。我使用 class 和带有静态方法的静态 ObservableCollection 来维护列表。一切正常。
我无法访问列表框项目中的复选框。我尝试了在 Stack Overflow 和其他地方找到的多种想法。我尝试了很多东西,一一列举就太长了。
这是最新的尝试。
当我将复选框从代码中删除时,它会起作用。
我知道我无法访问复选框,它没有被识别为列表项的一部分。
但我就是不知道怎么解决。
<ListBox x:Name="ListBox1" ItemsSource="{Binding}"
DoubleTapped="ListBox1_DoubleTapped" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="checkBox" Checked="CheckBox_Checked"
IsChecked="{Binding Picked}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public sealed partial class MainPage : Page
{
Binding myBinding = new Binding();
public MainPage()
{
//....stuff
ListBox1.DataContext = MyList.list;
//... etc
// This works!!
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Update listbox after item has been added and
// user has returned to main page.
ListBox1.UpdateLayout();
}
// This works if I leave the checkbox out of it!!
private void ListBox1_DoubleTapped(object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
CheckBox checkBox = (CheckBox)this.FindName("checkBox");
// Find the index;
int i = ListBox1.SelectedIndex;
// Some stuff..
// This is what is bound to the checkbox in the xaml!!
item.Picked = true;
try
{
//Manually trying to change the checked of the checkbox!!
// Yes increasingly desperate!!
checkBox.IsChecked = true;
}// Necessary as the checkbox is always throwing this.
catch (NullReferenceException e) { }
}
// Alter Picked value to true on checkbox selection.
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
// Find the index;
//THE INDEX IS ALWAYS 1!!!
int i = ListBox1.SelectedIndex;
try
{
//Trying again to manually manipulate, even though
//the data is supposed to be bound.
item.Picked = true;
}
catch (NullReferenceException ex){}
}
我尽量只包含基本信息。
我省略了边距、颜色等和基本声明等以尝试减少代码。
我想我会提供一个答案来详细说明已接受的答案 link 为我解决了这个问题。
基本上,link 提供了一种使用可视化树遍历 xaml 层次结构以在 ListBox 中查找控件的好方法。所以我的 xaml 看起来像这样:
<ListBox x:Name="ListBox1" ...>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel ...>
<CheckBox x:Name="checkBox" ... />
<TextBlock ... />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
// To select list item and change Picked value to true.
private void ListBox1_DoubleTapped(object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
// Find the index;
int i = ListBox1.SelectedIndex;
CheckBox checkBox = getCheckBox(ListBox1);
try
{
// Change Picked bool value.
item.Picked = true;
// Check CheckBox to show item selected.
checkBox.IsChecked = true;
}
catch (NullReferenceException exc) { }
}
// Taken and modified from Jerry Nixon. http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html
// Find the checkBox for that ListBox item.
CheckBox getCheckBox(ListBox ListBox1)
{
var _ListBoxItem = ListBox1.SelectedItem;
var _Container = ListBox1.ItemContainerGenerator.ContainerFromItem(_ListBoxItem);
var _Children = AllChildren(_Container);
var _Name = "checkBox";
var _Control = (CheckBox)_Children.First(c => c.Name == _Name);
return _Control;
}
// Taken and modified from Jerry Nixon. http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html
// Get any child controls from ListItem Container.
// Using a visual tree to access elements on the page
// within the xaml heirarchy of nested elements/tags.
public List<Control> AllChildren(DependencyObject parent)
{
var _List = new List<Control> { };
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is Control)
_List.Add(_Child as Control);
_List.AddRange(AllChildren(_Child));
}
return _List;
}
基本上这可以用于其他事件和控件。有用的代码。
我的项目 class 已选择字符串名称和布尔值。我使用 class 和带有静态方法的静态 ObservableCollection 来维护列表。一切正常。
我无法访问列表框项目中的复选框。我尝试了在 Stack Overflow 和其他地方找到的多种想法。我尝试了很多东西,一一列举就太长了。
这是最新的尝试。
当我将复选框从代码中删除时,它会起作用。
我知道我无法访问复选框,它没有被识别为列表项的一部分。
但我就是不知道怎么解决。
<ListBox x:Name="ListBox1" ItemsSource="{Binding}"
DoubleTapped="ListBox1_DoubleTapped" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="checkBox" Checked="CheckBox_Checked"
IsChecked="{Binding Picked}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public sealed partial class MainPage : Page
{
Binding myBinding = new Binding();
public MainPage()
{
//....stuff
ListBox1.DataContext = MyList.list;
//... etc
// This works!!
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Update listbox after item has been added and
// user has returned to main page.
ListBox1.UpdateLayout();
}
// This works if I leave the checkbox out of it!!
private void ListBox1_DoubleTapped(object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
CheckBox checkBox = (CheckBox)this.FindName("checkBox");
// Find the index;
int i = ListBox1.SelectedIndex;
// Some stuff..
// This is what is bound to the checkbox in the xaml!!
item.Picked = true;
try
{
//Manually trying to change the checked of the checkbox!!
// Yes increasingly desperate!!
checkBox.IsChecked = true;
}// Necessary as the checkbox is always throwing this.
catch (NullReferenceException e) { }
}
// Alter Picked value to true on checkbox selection.
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
// Find the index;
//THE INDEX IS ALWAYS 1!!!
int i = ListBox1.SelectedIndex;
try
{
//Trying again to manually manipulate, even though
//the data is supposed to be bound.
item.Picked = true;
}
catch (NullReferenceException ex){}
}
我尽量只包含基本信息。 我省略了边距、颜色等和基本声明等以尝试减少代码。
我想我会提供一个答案来详细说明已接受的答案 link 为我解决了这个问题。
基本上,link 提供了一种使用可视化树遍历 xaml 层次结构以在 ListBox 中查找控件的好方法。所以我的 xaml 看起来像这样:
<ListBox x:Name="ListBox1" ...>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel ...>
<CheckBox x:Name="checkBox" ... />
<TextBlock ... />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
// To select list item and change Picked value to true.
private void ListBox1_DoubleTapped(object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
// Find the index;
int i = ListBox1.SelectedIndex;
CheckBox checkBox = getCheckBox(ListBox1);
try
{
// Change Picked bool value.
item.Picked = true;
// Check CheckBox to show item selected.
checkBox.IsChecked = true;
}
catch (NullReferenceException exc) { }
}
// Taken and modified from Jerry Nixon. http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html
// Find the checkBox for that ListBox item.
CheckBox getCheckBox(ListBox ListBox1)
{
var _ListBoxItem = ListBox1.SelectedItem;
var _Container = ListBox1.ItemContainerGenerator.ContainerFromItem(_ListBoxItem);
var _Children = AllChildren(_Container);
var _Name = "checkBox";
var _Control = (CheckBox)_Children.First(c => c.Name == _Name);
return _Control;
}
// Taken and modified from Jerry Nixon. http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html
// Get any child controls from ListItem Container.
// Using a visual tree to access elements on the page
// within the xaml heirarchy of nested elements/tags.
public List<Control> AllChildren(DependencyObject parent)
{
var _List = new List<Control> { };
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is Control)
_List.Add(_Child as Control);
_List.AddRange(AllChildren(_Child));
}
return _List;
}
基本上这可以用于其他事件和控件。有用的代码。