如何使用mvvm将数据从数据库保存到组合框中 [英] how to save data from database into combo box using mvvm

查看:62
本文介绍了如何使用mvvm将数据从数据库保存到组合框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从数据库输出到组合框,

从数据库中检索数据但它没有绑定到组合框中

所有值都存储到可观察的收藏,但它没有约束力



这里我的代码





<大> XAML

I wants get the out put from database into combo box,
retrieved data from the database but it's not bind into the combo box
all the values are stored into observable collection but it's not binding

here my Code


XAML

<StackPanel Orientation="Horizontal" DataContext="{Binding Path=LocationListModel, Mode=TwoWay}"  >
                   <StackPanel Margin="5" >
                       <Label Content="Location Name"/>
                       <!--<TextBox TextWrapping="Wrap" Name="cmbLocation" Text="{Binding LocationNameLists, Mode=OneWay}" ></TextBox>-->
                       <ComboBox Name="cmbLocation"  Grid.Column="1" Grid.Row="1" Width="200" ItemsSource="{Binding LocationNameList}"  DisplayMemberPath="LocationName"  Loaded="cmbLocation_Loaded">
                           <!--<ComboBoxItem Content="qwerty"/>-->
                       </ComboBox>
                   </StackPanel>
                   <StackPanel Margin="5">
                       <Label Content="Building Name"/>
                       <ComboBox Name="cmbBuilding"  Width="200"/>
                   </StackPanel>
               </StackPanel>





WPF




公共类LocationListModel:paymentModel

{

/ / LocationEntity locationEntity = new LocationEntity();



private ObservableCollection< paymententity> LocationList = new ObservableCollection< paymententity>();

// public ObservableCollection< paymententity> LocationId = new ObservableCollection< paymententity>();



public ObservableCollection< paymententity> LocationNameList

{

get {return LocationList; }



set

{

LocationList = value;



base.RaisePropertyChangedEvent(LocationNameList);

}

}



public ObservableCollection< ; paymententity> LocationIdList

{

get;

套;



}





public void locationListModel()

{



PaymentEntity paymentEntity = new PaymentEntity();

PaymentBussiness paymentBussienss =新的PaymentBussiness();

DataTable LocationNameList = paymentBussienss.GetLocationList();



foreach(LocationNameList.Rows中的DataRow行)

{

var obj = new PaymentEntity()

{

LocationId =(int)row.ItemArray [0],

LocationName =(string)row.ItemArray [1]



};



LocationList.Add(obj);



}

base.RaisePropertyChangedEvent(LocationNameList);



}



}



WPF


public class LocationListModel : paymentModel
{
//LocationEntity locationEntity = new LocationEntity();

private ObservableCollection<paymententity> LocationList = new ObservableCollection<paymententity>();
// public ObservableCollection<paymententity> LocationId = new ObservableCollection<paymententity>();

public ObservableCollection<paymententity> LocationNameList
{
get { return LocationList; }

set
{
LocationList = value;

base.RaisePropertyChangedEvent("LocationNameList");
}
}

public ObservableCollection<paymententity> LocationIdList
{
get;
set;

}


public void locationListModel()
{

PaymentEntity paymentEntity = new PaymentEntity();
PaymentBussiness paymentBussienss = new PaymentBussiness();
DataTable LocationNameList = paymentBussienss.GetLocationList();

foreach (DataRow row in LocationNameList.Rows)
{
var obj = new PaymentEntity()
{
LocationId = (int)row.ItemArray[0],
LocationName = (string)row.ItemArray[1]

};

LocationList.Add(obj);

}
base.RaisePropertyChangedEvent("LocationNameList");

}

}

推荐答案

用以下代码替换你的组合框



Replace Your combobox with following code

<combobox name="cmbLocation" grid.column="1" grid.row="1" width="200"></combobox>



如果您在datlist中获取数据,那么您可以将这些数据绑定在这样的组合框中......

public void fillLocation()(当你想在组合框中绑定位置时调用这个方法)

{





cmbLocation.DataContext = dtSource;(你的数据表名称是什么)

cmbLocation.SelectedValuePath =ID;

cmbLocation.DisplayMemberPath =Name;

cmbLocation.UpdateLayout();

cmbLocation.SelectedValue = 0;

}





Happy Coding ............


If you are getting data in datlist then you can bind this data in combobox like that...
public void fillLocation()(Call this Method when you want to bind Location in combobox)
{


cmbLocation.DataContext = dtSource;(your data table name whatever it is)
cmbLocation.SelectedValuePath = "ID";
cmbLocation.DisplayMemberPath = "Name";
cmbLocation.UpdateLayout();
cmbLocation.SelectedValue = 0;
}


Happy Coding............


数据上下文设置不正确。

这个示例显示如何成功绑定到ViewModel。



此外:

1)模型不能用作数据上下文。

数据上下文总是一个ViewModel

2)在MVVM中使用代码是不好的做法

你应该使用命令。



MainWindow .xaml:

The data context was not set correctly.
This example shows you how you can successfully bind to a ViewModel.

Besides:
1) A model cannot be used as data context.
Data context is always a ViewModel
2) It is not good practice to use code behind in MVVM
You should use Commands instead.

MainWindow.xaml:
<Window x:Class="WpfApplication14.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:local="clr-namespace:WpfApplication14"

        Title="MainWindow" Height="350" Width="525">
    
    <Window.DataContext>
        <local:ViewModel></local:ViewModel>
    </Window.DataContext>
        
    <Grid>
        <StackPanel Orientation="Horizontal">
            <StackPanel Margin="5" >
                <Label Content="Location Name"/>               
                <ComboBox Name="cmbLocation"  Grid.Column="1" Grid.Row="1" Width="200" ItemsSource="{Binding LocationNameList}">                    
                </ComboBox>
            </StackPanel>
            <StackPanel Margin="5">
                <Label Content="Building Name"/>
                <ComboBox Name="cmbBuilding"  Width="200"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>





ViewModel.cs



ViewModel.cs

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfApplication14
{
    class ViewModel : INotifyPropertyChanged
    {
        [AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
        public sealed class CallerMemberNameAttribute : Attribute { }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        public void OnPropertyChanged([CallerMemberName]string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private ObservableCollection<string> locationNameList;

        public ObservableCollection<string> LocationNameList
        {
            get
            {
                return locationNameList;
            }
            set
            {
                if (locationNameList != value)
                {
                    locationNameList = value;

                    OnPropertyChanged();
                }
            }
        }

        public ViewModel()
        {
            locationNameList = new ObservableCollection<string>();

            LocationNameList.Add("London");
            LocationNameList.Add("Berlin");
            LocationNameList.Add("Paris");
            LocationNameList.Add("Mumbai");
            LocationNameList.Add("Toronto");
        }
    }
}


这篇关于如何使用mvvm将数据从数据库保存到组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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