【发布时间】:2019-11-07 10:19:32
【问题描述】:
我对 MVVM 比较陌生,我想将我的视图绑定到视图模型。我有很多代码要从 CodeBehind 移到 ViewModel 类中。
我想做的是将 ComboBox 事件绑定到相应的 ViewModel ICommand 方法。我希望 ComboBox 在视图加载时显示“CompanyB”,当我进行选择时,ComboBox 应该给我“CompanyA”、“CompanyB”和“CompanyC”作为选项可供选择。
选择公司后,下面2个文本框的值
Nachbest.Empf_Ansprechpartner
Nachbest.Empfaenger_Mail
必须相应改变。
问题在于我的代码,ComboBox 和文本框都是空的,而且在组合框中也没有任何选择。
你能帮我找到我在这里缺少的东西吗?提前感谢您的帮助!
XAML (neueNachbestellung.xaml):
<Window xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<Grid>
<StackPanel Grid.Column="0" Margin="25,25,0,0" x:Name="leftStPnl">
<ComboBox x:Name="cboxEmpfaenger"
ItemsSource="{Binding Empf}"
Text="{Binding Empfaenger}"
FontSize="12" Width="150" Margin="118,0,0,0"
SelectedItem="{Binding SelValue}">
</ComboBox>
<TextBox x:Name="txtEmpfAnsprechpartner" Text="{Binding Empf_Ansprechpartner}" FontSize="12" IsEnabled="False" Width="150" Margin="50,0,0,0"/>
<TextBox x:Name="txtEmpfMail" Text="{Binding Empfaenger_Mail}" FontSize="12" IsEnabled="False" Width="150" Margin="73,0,0,0"/>
</StackPanel>
</Grid>
</Window>
代码隐藏 (neueNachbestellung.xaml.cs):
public neueNachbestellung(string someId)
{
InitializeComponent();
this.DataContext = new neueNachbestellungViewModel(someId);
}
视图模型(neueNachbestellungViewModel.cs):
public class neueNachbestellungViewModel: INotifyPropertyChanged
{
public ICommand LoadCombobox => new DelegateCommand<object>(ExecuteLoadCombobox);
public ICommand ComboboxSelectionChanged => new DelegateCommand<object>(ExecuteComboboxSelectionChanged);
public Nachbestellung Nachbest { get; set; }
private object someObject;
private ObservableCollection<string> _empf;
public ObservableCollection<string> Empf
{
get { return _empf; }
set
{
_empf = value;
OnPropertyChanged("Empf");
}
}
private string _selValue = "12";
public string SelValue
{
get { return _selValue; }
set
{
_selValue = value;
OnPropertyChanged("SelValue");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public neueNachbestellungViewModel(string id)
{
this.Artikel = new ArtikelViewModel();
this.ArtikelList = new ObservableCollection<Artikel>();
InitializeReorderModel(id);
ExecuteComboboxSelectionChanged(someObject);
}
public void InitializeReorderModel(string id)
{
//set the MODEL
this.Nachbest = new Nachbestellung();
//Retrieve and set some values on *VIEW LOAD*!
var dbOracle = new Datenbank();
this.Nachbest.Bv = dbOracle.GetBauvorhaben(hv);
this.Nachbest.Hv = hv;
this.Nachbest.Bauleiter = dbOracle.GetBauleiter(hv);
this.Nachbest.Projektleiter = dbOracle.GetProjektleiter(hv);
}
private void ExecuteLoadCombobox(object param)
{
Empf = new ObservableCollection<string>()
{
"CompanyA",
"CompanyB",
"CompanyC"
};
//Company B is the standard selection on combobox load
Nachbest.Empf_Ansprechpartner = "CompanyB";
Nachbest.Empfaenger_Mail = "orders@companyB.com";
}
private void ExecuteComboboxSelectionChanged(object param)
{
Empf = new ObservableCollection<string>()
{
"CompanyA",
"CompanyB",
"CompanyC"
};
switch (SelValue)
{
case "CompanyA":
{
Nachbest.Empf_Ansprechpartner = "CompanyA";
Nachbest.Empfaenger_Mail = "service@companyA.com";
}
break;
case "CompanyB":
{
Nachbest.Empf_Ansprechpartner = "CompanyB";
Nachbest.Empfaenger_Mail = "orders@companyB.com";
}
break;
case "CompanyC":
{
Nachbest.Empf_Ansprechpartner = "CompanyC";
Nachbest.Empfaenger_Mail = "info@companyC.com";
}
break;
default:
MessageBox.Show("Something went wrong with the company selection!");
break;
}
}
}
查看片段:
【问题讨论】:
-
当你已经有一个
SelectedValue绑定时,绑定ComboBox 的SelectionChanged事件似乎没有意义。当SelValue视图模型属性的设置器已经被调用时,为什么要触发命令?另请注意,您通常会将 SelectedValue 与 SelectedValuePath 结合使用。否则绑定 SelectedItem。 -
此外,当您在运行时更改
Empf属性值时,该属性必须触发更改通知,例如INotifyPropertyChanged 接口的 PropertyChanged 事件。对于应该触发 UI 更新的任何属性,都必须执行此操作。 -
完全错误。视图模型是实现该接口的典型位置。
-
删除所有
Interaction.Triggers并在视图模型构造函数中调用 ExecuteComboboxSelectionChanged。 -
您的 ComboBox 也获得了错误的 DataContext,因为您在其父 StackPanel 上有
DataContext="{Binding Nachbest}。也删除它。