In the latest WP project, you need to import the mobile phone contact, and you can check "add" (the WP access contact is not mentioned here). The design is to add the CheckBox in the listemplate and The ListBox outside, of course, the interface design is completely okay and pretty, but when I went to the background to read data, I found that I was at a loss and had to worry about it for more than an hour, finally, I used ListBoxWithCheckBoxes to replace my previous ones. The results are very simple and convenient to use.
However, I recently accidentally saw the logic tree and the visual tree. on the Internet, the visual tree can traverse the template content (the visual tree concept also exists inWPF, It correspondsSilverlightThe visualization tree concept of is similar. However, a significant difference is thatWPFIt also provides an additional filter or object tree (called the "logic tree") concept .), I'm so excited. Now let's start our work and complete the CheckBox selection traversal in ListBox:
First define the data binding FileBrowserModel class:
public class FileBrowserModel : INotifyPropertyChanged { private string _FileName; public string FileName { get { return _FileName; } set { if (value != _FileName) { _FileName = value; NotifyPropertyChanged("FileName"); } } } private string _CreateDate; public string CreateDate { get { return _CreateDate; } set { if (value != _CreateDate) { _CreateDate = value; NotifyPropertyChanged("CreateDate"); } } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler(this, new PropertyChangedEventArgs(propertyName)); } } }
Note that the ItemsSource = "{Binding FileBrowserModels}" bound to ListBox is FileBrowserModels, which is:
public ObservableCollection<FileBrowserModel> FileBrowserModels { get; private set; }
Some code of XAML:
<ListBox x:Name="FileListBox" ItemsSource="{Binding FileBrowserModels}" Margin="0,-6,-12,0" Height="541"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock x:Name="tt" Text="{Binding CreateDate}"></TextBlock> <CheckBox Content="{Binding FileName}"></CheckBox> </StackPanel> </DataTemplate> </ListBox.ItemTemplate></ListBox>
After binding, add a Button in the Grid.
<Button x:Name="btn" Content="btn" Click="btn_Click"></Button>
The background code is as follows:
Private void btn_click (Object sender, routedeventargs e) {for (INT I = 0; I <this. fileListBox. items. count; I ++) {listboxitem item = This. fileListBox. itemcontainergenerator. containerfromindex (I) As listboxitem; // use visualtreehelper to locate textblock | checkbox control textblock txtblock = findlastelementinvisualtree <textblock> (item );
Checkbox chkbox = findfirstelementinvisualtree <checkbox> (item); // filebrowsermodel filebor = item. datacontext as filebrowsermodel; If (chkbox. ischecked = true) {MessageBox. show (chkbox. content. tostring () + txtblock. text. tostring ());}}}
Findfirstelementinvisualtree method:
View Code
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject { var count = VisualTreeHelper.GetChildrenCount(parentElement); if (count == 0) return null; for (int i = 0; i < count; i++) { var child = VisualTreeHelper.GetChild(parentElement, i); if (child != null && child is T) { return (T)child; } else { var result = FindFirstElementInVisualTree<T>(child); if (result != null) return result; } } return null; }
Findlastelementinvisualtree method:
View Code
private T FindLastElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject { var count = VisualTreeHelper.GetChildrenCount(parentElement); if (count == 0) return null; for (int i = count-1; i >= 0; i --) { var child = VisualTreeHelper.GetChild(parentElement, i); if (child != null && child is T) { return (T)child; } else { var result = FindFirstElementInVisualTree<T>(child); if (result != null) return result; } } return null; }
Since datatemplate has only two controls, one is traversing from the back to the back, and the other is traversing from the back to the back. In fact, his method is referring to an article abroad:
VisualTreeHelper
Through the above method, we can find the CheckBox selected in The ListBox and read the corresponding value. Of course, in the actual project, you may need to read more than just one CheckBox, is it necessary to add multiple textblocks to the DataTemplate ?? The answer is no. We actually have a CheckBox, which is enough. We can use FileBrowserModel filebor = item. dataContext as FileBrowserModel; (the FileBrowserModel here is the data binding class) obtains the currently selected object and reads other field values through filebor;
Finally in the MSDN reference learning: http://msdn.microsoft.com/zh-cn/library/bb613579.aspx
Finally, we will give you the running effect:
I am sorry for the fact that I am not very clear about the background. I am here to share my learning process with you. I hope you can make suggestions, make bricks, and learn and make progress together.