WPF(ContentControl和ItemsControl)

 

WPF(ContentControl和ItemsControl)

 分类:
 
[html] view plain copy
 
 print?
  1. <Window x:Class="TestOfContentControl.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="350" Width="525">  
  5.     <StackPanel>  
  6.         <Button Margin="5" >  
  7.             <TextBlock Text="Hello" />  
  8.         </Button>  
  9.           
  10.         <Button Margin="5" >  
  11.             <Image Source="j.png" Width="30" Height="30" />  
  12.         </Button>  
  13.           
  14.         <GroupBox Margin="10" BorderBrush="Gray" >  
  15.             <GroupBox.Header>  
  16.                 <Image Source="j.png" Width="20" Height="20" />  
  17.             </GroupBox.Header>  
  18.               
  19.             <TextBlock TextWrapping="WrapWithOverflow" Margin="5"   
  20.                        Text="一棵树,一匹马,一头大象和一只鸡在一起,打一种日常用品。"/>  
  21.         </GroupBox>  
  22.     </StackPanel>  
  23. </Window>  
[html] view plain copy
 
 print?
  1. <Window x:Class="TestOfItemsControl.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="350" Width="525">  
  5.   
  6.     <StackPanel>  
  7.         <ListBox Margin="5" >  
  8.             <CheckBox x:Name="checkBoxTim" Content="Tim" />  
  9.             <CheckBox x:Name="checkBoxTom" Content="Tom" />  
  10.             <CheckBox x:Name="checkBoxBruce" Content="Bruce" />  
  11.             <Button x:Name="buttonMess" Content="Mess" />  
  12.             <Button x:Name="buttonOwen" Content="Owen" />  
  13.             <Button x:Name="buttonVictor" Content="Victor" Click="buttonVictor_Click"/>  
  14.         </ListBox>  
  15.         <ListBox x:Name="empLists" >           
  16.         </ListBox>  
  17.     </StackPanel>    
  18. </Window>  

[csharp] view plain copy
 
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14.   
  15. namespace TestOfItemsControl  
  16. {  
  17.     /// <summary>  
  18.     /// Interaction logic for MainWindow.xaml  
  19.     /// </summary>  
  20.     public partial class MainWindow : Window  
  21.     {  
  22.         public MainWindow()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.   
  27.         private void buttonVictor_Click(object sender, RoutedEventArgs e)  
  28.         {  
  29.             List<Employee> empList = new List<Employee>()  
  30.                                      {  
  31.                                          new Employee(){Id=1,Name="Tim",Age = 30},  
  32.                                          new Employee(){Id=2,Name="Tom",Age=26},  
  33.                                          new Employee(){Id=3,Name="Guo",Age=26},  
  34.                                          new Employee(){Id=4,Name="Yan",Age=26},  
  35.                                          new Employee(){Id=5,Name="Owen",Age=26},  
  36.                                          new Employee(){Id=6,Name="Victor",Age=26}  
  37.                                          
  38.                                      };  
  39.             Button btn = sender as Button;  
  40.             DependencyObject level1 = VisualTreeHelper.GetParent(btn);  
  41.             DependencyObject level2 = VisualTreeHelper.GetParent(level1);  
  42.             DependencyObject level3 = VisualTreeHelper.GetParent(level2);  
  43.             MessageBox.Show(level3.GetType().ToString());  
  44.   
  45.             this.empLists.DisplayMemberPath = "Name";  
  46.             this.empLists.SelectedValuePath = "Id";  
  47.             this.empLists.ItemsSource = empList;  
  48.         }  
  49.     }  
  50.   
  51.     public class Employee  
  52.     {  
  53.         public int Id { get; set; }  
  54.         public string Name { get; set; }  
  55.         public int Age { get; set; }  
  56.     }  
  57. }  
原文地址:https://www.cnblogs.com/lizhenlin/p/5797234.html