简体   繁体   English

将Combobox项插入数据库列

[英]Insert Combobox items to database column

Is it possible to insert values from combobox to specific column in database? 是否可以将combobox值插入数据库中的特定列?

For example if i have combobox with items: item1 , item2 , item3 and I want to bind them to some column : column1 , what I want to manage is this: if item1 is selected, when I click on button , I want to insert value of that item in the column1 , otherwise if selected item is item2 then I want value of item2 to be inserted in column1 , etc... 例如,如果我有comboboxitem1item2item3 ,我想bind它们bind到某个columncolumn1 ,我想要管理的是:如果选择了item1 ,当我点击button ,我想插入值在列1该项目,否则,如果选定的是ITEM2的话,我想ITEM2的值在列1中插入,等等。

Now I know that the question is not really well written, but I just want to know if this is possible to do. 现在我知道问题写得不是很好,但我只是想知道这是否可行。

I've been Googling for this type of problem but I couldn't find the solution. 我一直在谷歌搜索这类问题,但我找不到解决方案。 I know how to make column records to be inserted into combobox items list but don't know the way to do the opposite. 我知道如何将column记录插入combobox项目列表但不知道相反的方法。

Also would like to say that I have this problem in my WPF/WCF/MVVM application, so I would like to know if this is possible (and how) to solve it that way. 还想说我的WPF / WCF / MVVM应用程序中有这个问题,所以我想知道这是否可能(以及如何)以这种方式解决它。

ofc you can. 你可以。 On the button click event you can just get the selected value of the combobox and save it. 在按钮单击事件上,您只需获取组合框的选定值并保存即可。

var selectedItem = ComboBoxName.SelectedItem;

But if you have binded the combobox with objects then you can cast it. 但是如果你将组合框与物体绑定在一起,那么就可以施放它。

var selectedItem = ComboBoxName.SelectedItem as (objecttypehere)

Update I missed that you used MVVM. 更新我错过了您使用MVVM。 Then in the view you can use bind the combobox with selected item. 然后在视图中,您可以使用将组合框与所选项目绑定。

<ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding TheSelectedItem}">
     //Itemtemplates.       
</ComboBox>

In the viewModel just acces the property you binded with the selecteditem in my test case "TheSelectedItem" property. 在viewModel中,只需在我的测试用例“TheSelectedItem”属性中访问与selecteditem绑定的属性。

Save it! 保存!

This solution is based on MVVM pattern . 此解决方案基于MVVM模式。 Bind the Selected item of combo box control to some property in View model. 将选定的组合框控件项绑定到View模型中的某个属性。 So your view should look like 所以你的观点应该是这样的

    <ComboBox ItemsSource="{Binding SomeItems,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}" SelectedValue="{Binding SelectedItemBinding,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}" />

    <Button Command={Binding ButtonClickCommand} ..../>

so once the Button is clicked you will get the RelayCommand handle in viewmodel and you can have a logic there to Get the selected item and use the value to insert into the column. 因此,一旦单击Button,您将在viewmodel中获得RelayCommand句柄,并且您可以在那里获取所选项目的逻辑并使用该值插入到列中。 Your view model should look like , 你的视图模型看起来像,

    public class ViewModel : //implement notify property changed and ICommand
    {
        public RelayCommand ButtonClickCommand
        {
             get new RelayCommand(EventHandlerToBeCalled);
        }


        public string SelectedItemBinding
        {
             get;
             set
             {
                  //notify property changed.
             }
        }

        //method called when button is clicked.
        private void EventHandlerToBeCalled()
        {
              //here set the SelectedItemBinding to a column.
        }
    }
  1. in combobox you can store values with prefix (col1_12, col2_24 etc) 在组合框中,您可以存储带前缀的值(col1_12,col2_24等)
  2. on button click you should parse value: separate it on prefix and original value 在按钮单击时,您应解析值:在前缀和原始值上分隔它
  3. so you can write value to needed column: 所以你可以写入所需列的值:

    switch (prefix) { case "col1" : table.column1 = value; switch(prefix){case“col1”:table.column1 = value; break; 打破; case "col2" : table.column2 = value; case“col2”:table.column2 = value; break; 打破; // etc } //等}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM