该解决方案基于 MVVM 模式。将组合框控件的 Selected 项绑定到 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.
}
}