TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Divyanshu Agarwal
NA
31
810
Change the selected Value of a combo box from a window button
Sep 26 2020 5:57 PM
How do I change the selected Value of a combo box from a window button when combo box is binded with enumList in its own viewmodel?
I have two Windows and several user controls in my project, one of the user control Ribbon is placed on MainWindow.
The Ribbon is groupBox user control containing a comboBox which is binded with enum PackageType as follows:
namespace
ManglamStellaris.ViewModels
{
public
class
RibbonViewModel : ViewModel
{
public
event
Action<PackageType> OnPackageSelection;
public
RibbonViewModel()
{
ListPackageType =
new
List<Tuple<PackageType,
string
>>(
new
Tuple<PackageType,
string
>[]
{
new
Tuple<PackageType,
string
>(PackageType.Raw,
"Raw"
),
new
Tuple<PackageType,
string
>(PackageType.Lagna,
"Lagna"
),
new
Tuple<PackageType,
string
>(PackageType.LaalKitab,
"LaalKitab"
)
});
}
public
List<Tuple<PackageType, String>> ListPackageType {
get
;
private
set
; }
public
PackageType packageType
{
get
{
return
_packageType; }
set
{
_packageType = value;
RaisePropertyChanged(
"packageType"
);
if
(OnPackageSelection !=
null
)
{
OnPackageSelection(_packageType);
}
}
}
private
PackageType _packageType;
}
}
An enum PackageType is defined in enums.cs
public
enum
PackageType
{
Raw,
Lagna,
LaalKitab
}
RibbonViewModel is initialized in MainViewModel as follows:
public
class
MainViewModel : ViewModel
{
Dictionary<Type,
object
> _Services =
new
Dictionary<Type,
object
>();
public
event
Action<PackageType> OnPackageSelection;
public
MainViewModel() {
Config =
new
ConfigViewModel();
Input =
new
InputViewModel();
Result =
new
CalculationResultViewModel();
RibbonViewModel =
new
RibbonViewModel();
RibbonViewModel.OnPackageSelection += (PackageType) => {
if
(OnPackageSelection !=
null
) {
OnPackageSelection(PackageType);
}
};
DoCalculationCommand =
new
RelayCommand(() => {
DoCalculation();
});
}
MainWindow controls the user control shown in the content control area of Main Window based on selection type:
public
partial
class
MainWindow : Window
{
public
MainViewModel ViewModel;
public
ResultView ResultView;
public
LagnaChart LagnaChart;
public
LaalKitab LaalKitab;
public
MainWindow(MainViewModel mainViewModel)
{
InitializeComponent();
ViewModel = mainViewModel;
ViewModel.OnPackageSelection += (PackageType) => {
if
(PackageType == PackageType.LaalKitab)
{
this
.mainContentArea.Content = LaalKitab;
}
else
if
(PackageType == PackageType.Lagna)
{
this
.mainContentArea.Content = LagnaChart;
}
else
if
(PackageType == PackageType.Raw)
{
this
.mainContentArea.Content = ResultView;
}
};
DataContext = ViewModel;
ResultView =
new
ResultView(ViewModel);
LagnaChart =
new
LagnaChart(ViewModel);
LaalKitab =
new
LaalKitab(ViewModel);
this
.mainContentArea.Content = ResultView;
}
}
How can I select the combo box value from another Window Button Click function which is as follows:
public
partial
class
HomeWindow : Window
{
public
LagnaChart LagnaChart;
MainViewModel _mainViewModel =
new
MainViewModel();
public
HomeWindow()
{
InitializeComponent();
_mainViewModel =
new
MainViewModel();
DataContext = _mainViewModel;
}
private
void
LaalKitab(
object
sender, RoutedEventArgs e)
{
MainWindow mainWindow =
new
MainWindow(_mainViewModel);
_mainViewModel =
new
MainViewModel();
DataContext = _mainViewModel;
mainWindow.Show();
}
}
Function LaalKitab opens a window but how to set the value of comboBox in the LaalKitab function itself and pass it so it changes the user control in Main Window content area?
It works perfectly when I change the value from comboDropdown at runtime.
Thanks in advance
Reply
Answers (
1
)
How to add conditions in data trigger in wpf
How do I convert image to byte array