1

Possible Duplicate:
How to use nested class in WPF XAML?

I am refractoring the code from sample:

And after excluding Skills class, with corresponding changes in
in MainWindow.xaml

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

I am having in MainWindow1.xaml.cs:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApplication
{
  public class Skill
  {//I'd like to remove class Skill having moved property string Description into class Employee 
    public string Description { get; set; }
  }

   public class Employee 
   {
    public string Name { get  ; set; }
    public int Age  { get; set; }
//I'd like to change in the next line Skill --> String               
    public List<Skill> Skills { get; set; }
    //public List<String> Skills { get; set; }

     public Employee()
     {
//I'd like to change in the next line Skill --> String       
       Skills=new List<Skill>();
       //Skills=new List<string>();
     }
  }

  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 Window1 : Window
    {
      public Window1()
    {
        InitializeComponent();
    }
  }
}

How should I change Window1.XAML if to change:

  • List<Skill> to List<String>

in Window1.xaml.cs?

Related questions based on the same code:

Update:
Just to note what I tried wrong:

  • "A" instead of A (without quotes);
  • it should be String (but not string) in XAML (while in C# either string or String)
  • tried to place A as attribute, according existed code, in one tag <sys:String /> instead of: <sys:String>A</sys:String>
Community
  • 1
  • 1

1 Answers1

2

Here goes your Employee class with new approach you wanted it to be like -

public class Employee
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<String> Skills { get; set; }

    public Employee()
    {
        Skills=new List<string>();
    }
}

You can create the instance in xaml files for the same like this -

        <local:Team x:Key="teams">
            <local:Employee Name="Larry" Age="21">
                <local:Employee.Skills>
                    <sys:String>A</sys:String>
                    <sys:String>B</sys:String>
                    <sys:String>C</sys:String>
                </local:Employee.Skills>
            </local:Employee>
        </local:Team>

You need to define sys namespace in your xaml to declare an instance of string in xaml -

xmlns:sys="clr-namespace:System;assembly=mscorlib"

I tested the above code with ListBox and its working -

<ListBox ItemsSource="{Binding Skills, Source={StaticResource teams}}"/>
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185