更新时间:2022-12-20 12:53:44
这样做。更改了组合框中的值后,检查viewmodel中Companies.CurrentItem的值。如果您有任何疑问,请告诉我。
XAML代码 -
Do Like this. After changes the value in combobox then check the value for "Companies.CurrentItem" in viewmodel. If you have any doubt please let me know.
XAML Code-
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="56,79,0,0"
ItemsSource="{Binding Companies, Mode=OneWay}"
DisplayMemberPath="Name"
SelectedValue = "{Binding SelectedItem.CompanyId, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
SelectedValuePath="CompanyId"
IsSynchronizedWithCurrentItem="True"
IsEditable="True" VerticalAlignment="Top" Width="222"/>
ViewModel Code-
ViewModel Code-
public class CompanyEntity
{
public int? CompanyId { get; set; }
public string Name { get; set; }
}
class MainWindowVM
{
ObservableCollection<CompanyEntity> companies = new ObservableCollection<CompanyEntity>();
ICommand saveCommand = null;
public MainWindowVM()
{
companies.Add(new CompanyEntity { CompanyId = 1,Name= "Microsoft" });
companies.Add(new CompanyEntity { CompanyId = 2,Name= "Apple" });
}
public ICommand SaveCommand
{
get
{
return saveCommand = new RelayCommand(Save);
}
}
private void Save()
{
}
public CollectionView Companies
{
get
{
return CollectionViewSource.GetDefaultView(companies) as CollectionView;
}
}
}