user3214034
user3214034

Reputation: 221

Call instance of class in Xaml

Help me understand. I create template of SilverlightApplication1 in VS2012. Create in my solution folder Models and add class EmployeeTypes.cs. I need to call up the instance of EmployeeTypes.cs in Xaml file (Home.xaml).

I do so in Home.xaml:

<navigation:Page x:Class="SilverlightApplication1.Home" 
     xmlns:local="clr-namespace:SilverlightApplication1.Models" 
                           ....
                           ....

Then i do so ( all this according tutorial http://weblogs.asp.net/psheriff/archive/2012/04/09/silverlight-tree-view-with-multiple-levels.aspx):

<local:EmployeeTypes x:Key=" employeeTemplate"/>  

Here VS2012 says: 'local:EmployeeTypes' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

Where i am wrong???

EmployeeTypes.cs

namespace SilverlightApplication1.Models
{
public class EmpolyeeTypes : List<EmployeeType>
{
    public EmpolyeeTypes()
    {
    EmployeeType type;
    type=new EmployeeType("Manager");
    type.Employees.Add(new Employee("Michael"));
    type.Employees.Add(new Employee("Paul"));
    this.Add(type);

    type = new EmployeeType("Project Managers");
    type.Employees.Add(new Employee("Tim"));
    type.Employees.Add(new Employee("John"));
    type.Employees.Add(new Employee("David"));
    this.Add(type);

    }


}

}

<navigation:Page x:Class="SilverlightApplication1.Home" 
xmlns:local="clr-namespace:SilverlightApplication1.Models"      
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
Title="Home"
Style="{StaticResource PageStyle}">
<local:EmployeeTypes x:Key=" employeeTemplate"/>
        .......
        .......


<Grid x:Name="LayoutRoot">

</Grid>

Upvotes: 1

Views: 568

Answers (1)

har07
har07

Reputation: 89285

Try to move it to Resources of one UI control, for example as Resources of Grid :

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
        <local:EmployeeTypes x:Key=" employeeTemplate"/>
    </Grid.Resources>
</Grid>

or as Resources of the page :

<navigation:Page 
    ......
    >
    <navigation:Page.Resources>
        <local:EmployeeTypes x:Key=" employeeTemplate"/>
    </navigation:Page.Resources>
</navigation:Page>

PS: my answer came from Windows Phone and WPF background, could be different in silverlight. Not sure.

Upvotes: 2

Related Questions