WPF(ContentControl和ItemsControl)

<Window x:Class="TestOfContentControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button Margin="5" >
            <TextBlock Text="Hello" />
        </Button>
        
        <Button Margin="5" >
            <Image Source="j.png" Width="30" Height="30" />
        </Button>
        
        <GroupBox Margin="10" BorderBrush="Gray" >
            <GroupBox.Header>
                <Image Source="j.png" Width="20" Height="20" />
            </GroupBox.Header>
            
            <TextBlock TextWrapping="WrapWithOverflow" Margin="5" 
                       Text="一棵树,一匹马,一头大象和一只鸡在一起,打一种日常用品。"/>
        </GroupBox>
    </StackPanel>
</Window>
<Window x:Class="TestOfItemsControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <StackPanel>
        <ListBox Margin="5" >
            <CheckBox x:Name="checkBoxTim" Content="Tim" />
            <CheckBox x:Name="checkBoxTom" Content="Tom" />
            <CheckBox x:Name="checkBoxBruce" Content="Bruce" />
            <Button x:Name="buttonMess" Content="Mess" />
            <Button x:Name="buttonOwen" Content="Owen" />
            <Button x:Name="buttonVictor" Content="Victor" Click="buttonVictor_Click"/>
        </ListBox>
        <ListBox x:Name="empLists" >         
        </ListBox>
    </StackPanel>  
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestOfItemsControl
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void buttonVictor_Click(object sender, RoutedEventArgs e)
        {
            List<Employee> empList = new List<Employee>()
                                     {
                                         new Employee(){Id=1,Name="Tim",Age = 30},
                                         new Employee(){Id=2,Name="Tom",Age=26},
                                         new Employee(){Id=3,Name="Guo",Age=26},
                                         new Employee(){Id=4,Name="Yan",Age=26},
                                         new Employee(){Id=5,Name="Owen",Age=26},
                                         new Employee(){Id=6,Name="Victor",Age=26}
                                       
                                     };
            Button btn = sender as Button;
            DependencyObject level1 = VisualTreeHelper.GetParent(btn);
            DependencyObject level2 = VisualTreeHelper.GetParent(level1);
            DependencyObject level3 = VisualTreeHelper.GetParent(level2);
            MessageBox.Show(level3.GetType().ToString());

            this.empLists.DisplayMemberPath = "Name";
            this.empLists.SelectedValuePath = "Id";
            this.empLists.ItemsSource = empList;
        }
    }

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}


posted on 2013-04-01 16:25  Yours风之恋  阅读(272)  评论(0编辑  收藏  举报