当前位置: 代码迷 >> C# >> 新手再次问个WPF程序有关问题
  详细解决方案

新手再次问个WPF程序有关问题

热度:38   发布时间:2016-05-05 03:28:13.0
【求助】新手再次问个WPF程序问题
错误 1 “P13_3.Window1”不包含“listView1_SelectionChanged”的定义,并且找不到可接受类型为“P13_3.Window1”的第一个参数的扩展方法“listView1_SelectionChanged”(是否缺少 using 指令或程序集引用?) E:\C#\实验报告\实验六\P13_3\Window1.xaml 6 110 P13_3

实在不知道为何属性里面没有事件选项卡


cs源代码
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 P13_3
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        private School school;

        public Window1()
        {
            InitializeComponent();
            Office o1 = new Office("计算机基础", "王军", "杨小勇", "何平", "姜涛");
            Office o2 = new Office("软件工程", "马建国", "陈君", "刘小燕");
            Office o3 = new Office("信息安全", "冯尧", "李建军", "张涛");
            Department d1 = new Department("计算机", o1, o2, o3);
            Office o4 = new Office("自动控制", "吴自力", "陈峰", "薛小龙");
            Office o5 = new Office("工业设计", "吴淑华", "方坤", "何力", "蔡聪");
            Department d2 = new Department("机电工程", o4, o5);
            Office o6 = new Office("信息管理", "赵民", "盛小楠", "徐小平");
            Office o7 = new Office("工商管理", "张敏", "李玲", "吕倩", "高剑");
            Department d3 = new Department("经济管理", o6, o7);
            school = new School("交通大学", d1, d2, d3);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TreeViewItem root = new TreeViewItem() { Header = school };
            treeView1.Items.Add(root);
            foreach (Department d in school.Departments)
            {
                TreeViewItem item = new TreeViewItem() { Header = d };
                root.Items.Add(item);
                foreach (Office o in d.Offices)
                    item.Items.Add(new TreeViewItem() { Header = o });
            }
        }

        private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            listView1.Items.Clear();
            if (treeView1.SelectedItem == null)
                return;
            TreeViewItem item = (TreeViewItem)treeView1.SelectedItem;
            if (item.Header is School)
                foreach (Department d in school.Departments)
                    listView1.Items.Add(d);
            else if (item.Header is Department)
                foreach (Office o in ((Department)(item.Header)).Offices)
                    listView1.Items.Add(o);
            else if (item.Header is Office)
                foreach (string s in ((Office)item.Header).Teachers)
                    listView1.Items.Add(s);
        }
    }

    public class School
    {
        private string name;
        public string Name
        {
            get { return name; }
        }

        private List<Department> departments;
        public List<Department> Departments
        {
            get { return departments; }
        }

        public School(string name, params Department[] departments)
        {
            this.name = name;
            this.departments = new List<Department>(departments);
        }

        public override string ToString()
        {
            return name;
        }
    }

    public class Department
    {
        private string name;
        public string Name
        {
            get { return name; }
        }

        private List<Office> offices;
        public List<Office> Offices
        {
            get { return offices; }
        }

        public Department(string name, params Office[] offices)
        {
            this.name = name;
            this.offices = new List<Office>(offices);
        }

        public override string ToString()
        {
            return name + "系";
        }
    }

    public class Office
    {
        private string name;
        public string Name
        {
            get { return name; }
        }

        private List<String> teachers;
        public List<String> Teachers
        {
            get { return teachers; }
        }

        public Office(string name, params string[] teachers)
        {
            this.name = name;
            this.teachers = new List<string>(teachers);
        }

        public override string ToString()
        {
            return name + "教研室";
        }
    }
}
XAML源代码
<Window x:Class="P13_3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="286" Width="529">
    <Grid>
        <ListView HorizontalAlignment="Left" Name="listView1" Width="247" SelectionChanged="listView1_SelectionChanged" />
        <TreeView Margin="246,0,0,0" Name="treeView1" />
    </Grid>
</Window>

------解决思路----------------------
listView1_SelectionChanged
这个方法你写在哪里了?
------解决思路----------------------
呵,你选择的是 Grid,它怎么会有SelectionChanged事件。
  相关解决方案