Reputation: 2003
In my ComboBox I have few different languages which are loaded from a database (through Binding). When the selection in the ComboBox changes to a different language and the User clicks on the OK button, I want to do something simple like show a new window. I want to realize this with a WPF Commands (Name_CanExecute and Name_Executed). I was playing all day yesterday and couldn't make it work. I am new to Wpf and MVVM pattern.
I would be very thankful if someone could show me how to do it. Any help is appreciated.
My .xaml code in the View looks like this:
<UserControl x:Class="***.***.Modules.Localization.Views.LocalizationManager"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:***.***.Modules.Localization.Services"
xmlns:viewModels="clr-namespace:***.***.Modules.Localization.ViewModels"
Height="270" Width="230"
mc:Ignorable="d">
<UserControl.Resources>
<viewModels:LocalizationViewModelLocator x:Key="LocalizationViewModelLocator"/>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Mode="OneWay" Path="LocalizationViewModel" Source="{StaticResource LocalizationViewModelLocator}"/>
</UserControl.DataContext>
<Grid Background="Gainsboro">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="1" Margin="10,10">
<TextBlock FontSize="13" FontFamily="Verdana" Text="Language: " />
<ComboBox Name="CboLanguageSelector" BorderThickness="1" MinWidth="105" ItemsSource="{Binding Languages}" />
<Button>OK</Button>
</StackPanel>
</Grid>
and my code in ViewModel looks like this:
public class LocalizationViewModel : LocalizableViewModel
{
//the available languages from database are saved here
public ObservableCollection<string> Languages { get; private set; }
private readonly IDataAccessUnitOfWorkFactory dataAccessUnitOfWorkFactory;
public LocalizationViewModel(ILocalizationService localizationService,
IDataAccessUnitOfWorkFactory dataAccessUnitOfWorkFactory) : base(localizationService)
{
this.dataAccessUnitOfWorkFactory = dataAccessUnitOfWorkFactory;
LoadLanguagesFromCtlangTable();
}
/// <summary>
/// Get the available languages from the CTLANG table
/// </summary>
private void LoadLanguagesFromCtlangTable()
{
using (var unitOfWork = dataAccessUnitOfWorkFactory.Create())
{
string query = "SELECT languagename FROM ctlang ORDER BY languagename";
Languages = new ObservableCollection<string>(unitOfWork.OwEntities.Database.SqlQuery<string>(query));
}
}
//Implement Command_Execute and Command_CanExecute
}
Upvotes: 2
Views: 3729
Reputation: 185235
Bind ComboBox.SelectedItem
to a property on your view model, in your command you can then just read that property value, no need for additional events. If you have no separate command and just want to execute code on change: Place said code in the setter of the bound property.
Upvotes: 3