我们如何根据使用XAML的对象的字段值的值在wpf页面上显示不同的字段。 [英] How we can show different fields on a wpf page depending on the value of a field value of an object using XAML.

查看:75
本文介绍了我们如何根据使用XAML的对象的字段值的值在wpf页面上显示不同的字段。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个类Employee包含像



EmpId

EmpName

EmpAddress

EmpWorkNature

EmpAppraisal

EmpDesignation(它可能包含三个值enum,高级经理,助理经理,初级工程师)



现在在wpf页面上,有一个员工姓名的下拉列表。

问题是 - 选择一个员工后,应该打开一个wpf页面,或者在同一个wpf页面上应该有一个部分,根据他的名称显示员工的不同字段。



意味着



如果选择的员工是高级经理,只有EmpID,EmpName和EmpAdd应该是可见的。



如果选择的员工是Ass Manager,则只有EmpID,EmpName和EmpWorkNature可见。



如果所选员工是初级工程师只有EmpID,EmpName和EmpAppraisal应该是可见的。

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.

推荐答案

- 定义3 DataTemplates [ ^ ]

- 定义 ContentControl [ ^ ]其中内容 [ ^ ] -Property绑定到员工实例。

- 定义 Style [ ContentControl的> ^ ] [ ^ ]。在此样式 [ ^ ]定义 DataTriggers [ ^ ]检查EmpDesignation-Property并分配相应的DataTemplate [ ^ ]到 ContentControls [ ^ ]。 ContentTemplate [ ^ ] - 属性:



- 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:

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


这篇关于我们如何根据使用XAML的对象的字段值的值在wpf页面上显示不同的字段。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆