问题
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