基本的MVVM WPF没有填充数据

时间:2016-04-29 01:31:31

标签: wpf

嗨 - 我是WPF MVVM的新手。我正在尝试下面的代码。

MainWindow.xaml:

<Window x:Class="BasicMVVMWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
      mc:Ignorable="d"    
      xmlns:vm="clr-namespace:BasicMVVMWPF.ViewModel" 
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <vm:SearchEmpVM />
</Window.DataContext>
<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="auto" ></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition  Height="auto" ></RowDefinition>
    </Grid.RowDefinitions>


    <StackPanel>
        <Grid Margin="0,51,0,-48" Grid.RowSpan="4">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"></RowDefinition>
                <RowDefinition Height="auto"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>
            <Label   Grid.Row="0" Grid.Column="0" Content="EmpId:"/>
            <TextBox x:Name="txtEmpId1"   Text="{Binding  ElementName=Window,Path=DataContext.EmpId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="0" Grid.Column="1" ></TextBox>
            <StackPanel DataContext="{Binding SearchCls}" Grid.Column="1" Grid.Row="1">
                <GroupBox>
                    <GroupBox.HeaderTemplate>
                        <DataTemplate>
                            <Label    Content="Employee Information"/>

                        </DataTemplate>
                    </GroupBox.HeaderTemplate>
                    <Grid >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="26*"/>
                            <RowDefinition Height="26*"/>
                            <RowDefinition Height="26*"/>
                            <RowDefinition Height="26*"/>
                            <RowDefinition Height="26*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Label   Grid.Row="0" Grid.Column="0" Content="Name:" Grid.ColumnSpan="3" />
                        <TextBox  Text="{Binding Name}" Grid.Row="0" Grid.Column="3" Width="174"/>
                        <Label   Grid.Row="1" Grid.Column="0" Content="Designation:" Grid.ColumnSpan="3"/>
                        <TextBox Text="{Binding Designation}" Grid.Row="1" Grid.Column="3" Width="174"/>

                        <Label   Grid.Row="2" Grid.Column="0" Content="Department:" Grid.ColumnSpan="3" />
                        <TextBox  Text="{Binding Department}"  Grid.Row="2" Grid.Column="3" Width="174"/>

                    </Grid>



                </GroupBox>
            </StackPanel>

        </Grid>

    </StackPanel>


</Grid>

EmpCls.cs

namespace BasicMVVMWPF.Entity
{
    class EmpCls
    {
        private int _empNo;
        public int EmpNo
        {
            get
            {
                return _empNo;
            }
            set
            {
                _empNo = value;
            }
        }
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _designation;
        public string Designation
        {
            get
            {
                return _designation;
            }
            set
            {
                _designation = value;
            }
        }
        private string _department;
        public string Department
        {
            get
            {
                return _department;
            }
            set
            {
                _department = value;
            }
        }  
    }
}

SearchEmpVM.cs

namespace BasicMVVMWPF.ViewModel
{
    class SearchEmpVM : INotifyPropertyChanged 
    {
        List<EmpCls> EmpList = new List<EmpCls>();

        public SearchEmpVM()
        {
            // Add sample employee details into employee list    
            EmpList.Clear();
            EmpList.Add(new EmpCls { EmpNo = 1, Name = "John", Department = "IT", Designation = "Developer" });
            EmpList.Add(new EmpCls { EmpNo = 2, Name = "Mark", Department = "IT", Designation = "Tester" });
            EmpList.Add(new EmpCls { EmpNo = 3, Name = "Robert", Department = "IT", Designation = "DB Developer" });

        }
        #region properties

        private EmpCls _searchcls = new EmpCls();
        public EmpCls SearchCls
        {
            get { return _searchcls; }

            set
            {
                _searchcls = value;

                RaisePropertyChanged("SearchCls");

            }
        }

        private int _empid;
        public int EmpId
        {
            get
            {
                return _empid;

            }

            set
            {
                _empid = value;

                RaisePropertyChanged("EmpId");
                PopulteEmpDetails(_empid);
            }


        }


        #endregion

        private void PopulteEmpDetails(int _empid)
        {

            SearchCls = EmpList.Where(x => x.EmpNo == _empid).Single();

        }


        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion      
    }
}

在第一个文本框中执行项目时,我输入1并按下回车键。我没有看到数据被搜索和填充。请帮忙。

1 个答案:

答案 0 :(得分:1)

您对TextBox txtEmpId1的绑定是错误的。 ElementName用于按名称引用元素(例如txtEmpId1是元素名称)。

您已将DataContext设置为SearchEmpVM的实例,以便您可以直接参考其中的属性。如果你改变

,它会起作用
Text="{Binding  ElementName=Window,Path=DataContext.EmpId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"

Text="{Binding EmpId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

另外请注意,当您使用UpdateSourceTrigger=PropertyChanged时,只要您更改了值,就会加载字段。您也可以省略它并使用默认值(对于此属性等于UpdateSourceTrigger=LostFocus),当您跳出TextBox时它会加载数据。您可以在MSDN获取不同UpdateSourceTrigger值的列表。