How to change the WPF XAML to reflect changes in the C# code-behind?

筅森魡賤 提交于 2019-12-02 20:57:30

问题


I am refractoring the code from sample:

  • 24.129.21. Master Detail Binding
    from C# / CSharp Tutorial » Windows Presentation Foundation » Binding)

I came to the following C# code-behind, running with the same XAML:

namespace WpfApplication1 
{
    public class Skill
      {
        public string Description { get; set; }
      }

      //I'd like to comment out the next line
      public class Skills : ObservableCollection<Skill>{  }

        public class Employee 
      {
        public string Name { get  ; set; }
        public int Age  { get; set; }
        //public Skills Skills { get; set; }
        //instead of line above - works!
        public ObservableCollection<Skill> Skills { get; set; }
      }

      public class Team : ObservableCollection<Employee> { }

      public class Company
      {
        public string CompanyName { get  ; set; }
        public Team Members { get  ; set; }
      }

      public class Companies : ObservableCollection<Company> { }

      public partial class MainWindow : Window
        {
        public MainWindow()
      {
          InitializeComponent();
      }
    }
}

i.e. changed from

  • public Skills Skills { get; set; }

to

  • public ObservableCollection<Skill> Skills { get; set; }

in Employee class

Consequently, I'd like to get rid of, i.e. to comment out the line:

public class Skills : ObservableCollection<Skill>{  }

but I'm getting:

Error 1 The tag 'Skills' does not exist in XML namespace 'clr-namespace:_24_129_21_Master_Detail_Binding'. Line 13 Position 34

in Window1.xaml

<local:Team>
  <local:Employee Name="Larry" Age="21">
    <local:Employee.Skills>
       <local:Skills>

How can I change XAML in order to do it?

How should I change XAML for this?


回答1:


In your Window1.xaml class remove the reference to the skills class: (e.g. <local:Skills> ) since you don't have a Skills class anymore. Instead you want a number of Skill elements inside your ObservableCollection:

<local:Team>
   <local:Employee Name="Larry" Age="21">
     <local:Employee.Skills>
        <local:Skill />
        <local:Skill />
       ....

Also, you should probably initialize the Skills ObservableCollection inside the Employee's constructor.



来源:https://stackoverflow.com/questions/14536751/how-to-change-the-wpf-xaml-to-reflect-changes-in-the-c-sharp-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!