Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have a class Employee containing fields like

EmpId
EmpName
EmpAddress
EmpWorkNature
EmpAppraisal
EmpDesignation (It may contain three values of an enum, Sr. Manager, Ass. Manager, Junior Engineer )

Now on a wpf page, there is a drop down list of employee names.
Question is - After selecting an employee, there should open one wpf page or there should be a section on the same wpf page showing different fields of the employee based on his designation.

Means

if the employee selected is Sr Manager only EmpID, EmpName and EmpAdd should be visible.

if the employee selected is Ass Manager only EmpID, EmpName and EmpWorkNature should be visible.

if the employee selected is junior Engineer only EmpID, EmpName and EmpAppraisal should be visible.
Posted

1 solution

- Define 3 DataTemplates[^] for each visualization
- Define a ContentControl[^] which Content[^]-Property is bound to the employee instance.
- Define a Style[^] for this ContentControl[^]. In this Style[^] define DataTriggers[^] which examines the EmpDesignation-Property and assign the appropriate DataTemplate[^] to the ContentControls[^]. ContentTemplate[^]-Property:

XML
<Window
    x:Class="WpfApplication8.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication8"
    Title="MainWindow"
    Height="350"
    Width="525">
    <Grid>
        <Grid.DataContext>
            <x:ArrayExtension
                Type="local:Employee">
                <local:Employee
                    EmpId="1"
                    EmpName="EmpName1"
                    EmpAddress="EmpAddress1"
                    EmpWorkAppraisal="EmpWorkAppraisal1"
                    EmpWorkNature="EmpWorkNature1"
                    EmpDesignation="SeniorManager"></local:Employee>
                <local:Employee
                    EmpId="2"
                    EmpName="EmpName2"
                    EmpAddress="EmpAddress2"
                    EmpWorkAppraisal="EmpWorkAppraisal2"
                    EmpWorkNature="EmpWorkNature2"
                    EmpDesignation="AssManager"></local:Employee>
                <local:Employee
                    EmpId="3"
                    EmpName="EmpName3"
                    EmpAddress="EmpAddress3"
                    EmpWorkAppraisal="EmpWorkAppraisal3"
                    EmpWorkNature="EmpWorkNature3"
                    EmpDesignation="JuniorManager"></local:Employee>
            </x:ArrayExtension>
        </Grid.DataContext>
        <Grid.Resources>
            <!--implicit datetemplate, used when no template is spedcified explicitly for Employee-instances-->
            <DataTemplate
                DataType="{x:Type local:Employee}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock
                        Text="{Binding EmpId}"></TextBlock>
                    <TextBlock
                        Text=" - "></TextBlock>
                    <TextBlock
                        Text="{Binding EmpName}"></TextBlock>
                </StackPanel>
            </DataTemplate>
            
            <!--DataType can be omitted but is here for completeness-->
            <DataTemplate
                x:Key="DT_Employee_SenManager"
                DataType="{x:Type local:Employee}">
                <StackPanel>
                    <TextBlock
                        Text="{Binding EmpId}"></TextBlock>
                    <TextBlock
                        Text="{Binding EmpName}"></TextBlock>
                    <TextBlock
                        Text="{Binding EmpAddress}"></TextBlock>
                </StackPanel>
            </DataTemplate>
            <!--nice short form though ;) -->
            <DataTemplate
                x:Key="DT_Employee_AssManager"
                DataType="{x:Type local:Employee}">
                <StackPanel>
                    <TextBlock
                        Text="{Binding EmpId}"></TextBlock>
                    <TextBlock
                        Text="{Binding EmpName}"></TextBlock>
                    <TextBlock
                        Text="{Binding EmpWorkNature}"></TextBlock>
                </StackPanel>
            </DataTemplate>
            <DataTemplate
                x:Key="DT_Employee_JunManager"
                DataType="{x:Type local:Employee}">
                <StackPanel>
                    <TextBlock
                        Text="{Binding EmpId}"></TextBlock>
                    <TextBlock
                        Text="{Binding EmpName}"></TextBlock>
                    <TextBlock
                        Text="{Binding EmpWorkAppraisal}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>
        <StackPanel>
            <ComboBox
                Name="_cb"
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding}">
            </ComboBox>
            <ContentControl
                Content="{Binding /}">
                <ContentControl.Style>
                    <Style
                        TargetType="ContentControl">
                        <Style.Triggers>
                            <DataTrigger
                                Binding="{Binding EmpDesignation}"
                                Value="{x:Static local:EmployeeDesignation.SeniorManager}">
                                <Setter
                                    Property="ContentTemplate"
                                    Value="{StaticResource DT_Employee_SenManager}"></Setter>
                            </DataTrigger>
                            <DataTrigger
                                Binding="{Binding EmpDesignation}"
                                Value="{x:Static local:EmployeeDesignation.AssManager}">
                                <Setter
                                    Property="ContentTemplate"
                                    Value="{StaticResource DT_Employee_AssManager}"></Setter>
                            </DataTrigger>
                            <DataTrigger
                                Binding="{Binding EmpDesignation}"
                                Value="{x:Static local:EmployeeDesignation.JuniorManager}">
                                <Setter
                                    Property="ContentTemplate"
                                    Value="{StaticResource DT_Employee_JunManager}"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </StackPanel>
    </Grid>
</Window>
 
Share this answer
 
v3
Comments
manish.varsh 3-Mar-13 1:15am    
Thank you
earloc 4-Mar-13 3:06am    
you´re welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900