是否可以将combobox
中的值插入数据库中的特定列?
例如,如果我有combobox
项目: item1 , item2 , item3 ,我想{{1}他们对某些bind
: column1 ,我想要管理的是:如果选择 item1 ,当我点击column
时,我想要在 column1 中插入该项目的值,否则如果所选项目为 item2 ,则我希望 item2 的值插入 column1 等等......
现在我知道问题写得不是很好,但我只是想知道这是否可行。
我一直在谷歌搜索这类问题,但我找不到解决方案。我知道如何将button
条记录插入到column
项目列表中,但不知道如何做相反的事情。
还想说我的WPF / WCF / MVVM应用程序中存在这个问题,所以我想知道是否可以(以及如何)以这种方式解决它。
答案 0 :(得分:1)
var selectedItem = ComboBoxName.SelectedItem;
但是如果你将组合框与对象绑定在一起,那么就可以投射它。
var selectedItem = ComboBoxName.SelectedItem as (objecttypehere)
<强>更新强> 我错过了你使用MVVM。然后在视图中,您可以使用将组合框与所选项目绑定。
<ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding TheSelectedItem}">
//Itemtemplates.
</ComboBox>
在viewModel中,只需在我的测试用例“TheSelectedItem”属性中访问与selecteditem绑定的属性。
保存!
答案 1 :(得分:1)
此解决方案基于MVVM模式。 将选定的组合框控件项绑定到View模型中的某个属性。 所以你的观点看起来应该是
<ComboBox ItemsSource="{Binding SomeItems,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}" SelectedValue="{Binding SelectedItemBinding,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}" />
<Button Command={Binding ButtonClickCommand} ..../>
因此,一旦单击Button,您将在viewmodel中获得RelayCommand句柄,并且您可以在那里获取逻辑以获取所选项目并使用该值插入列中。您的视图模型应该是,
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.
}
}
答案 2 :(得分:0)
因此您可以将值写入所需的列:
switch(prefix){ case“col1”:table.column1 = value;打破; case“col2”:table.column2 = value;打破; //等 }