We have one silverlight application with MVVM Architecture.
in that i have one User Control (SearchUC.xaml) for binding combobox items inline. like below:
<usercontrol x:class="ComboxApp.SearchUC" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<grid x:name="LayoutRoot" removed="White">
<combobox x:name="CBCountry" grid.column="0" height="23" grid.row="0" width="130" selecteditem="{Binding Country,Mode=TwoWay}">
<comboboxitem tag="US" content="US" />
<comboboxitem tag="CA" content="CA" />
</combobox>
</grid>
</usercontrol>
And (SearchUC.xaml) Code behind is like below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ComboxApp;
namespace ComboxApp
{
public partial class SearchUC : UserControl
{
public SearchUC()
{
InitializeComponent();
MainPageViewModel mvm = new MainPageViewModel();
this.DataContext = mvm;
}
}
}
And i have one View (MainPage.xaml) here i am calling (SearchUC) UserControl and binding static Resource ViewModel (MainPageViewModel.cs) like below
<usercontrol x:class="ComboxApp.MainPage" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ComboxApp"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
xmlns:my="clr-namespace:ComboxApp">
<usercontrol.resources>
<local:mainpageviewmodel x:key="myviewmodel" xmlns:local="#unknown" />
</usercontrol.resources>
<grid x:name="LayoutRoot" removed="White">
<local:searchuc x:name="ucCustomerSearch" grid.row="0" datacontext="{Binding Source={StaticResource myviewmodel}}" xmlns:local="#unknown" /> <Button Content="Button"
Height="23"
HorizontalAlignment="Left"
Margin="123,188,0,0" Name="button1"
VerticalAlignment="Top"
Width="75" Command="{Binding Source={StaticResource myviewmodel},Path=GetSelect}" />
</grid>
</usercontrol>
And View model (MainPageViewModel.cs) as an below
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace ComboxApp
{
public class MainPageViewModel : INotifyPropertyChanged
{
public MainPageViewModel()
{
SelectedItem = new Country();
SelectedItem.Text = "US";
GetSelect = new CommandBase(GetSelectedItem);
}
private Country _selectedItem;
public Country SelectedItem
{
get { return _selectedItem; }
set { _selectedItem = value;
NotifyPropertyChanged("SelectedItem"); }
}
public ICommand GetSelect
{
get;
private set;
}
private void GetSelectedItem(object parameter)
{
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
}
when i select dropdown and click on button i want selected item in Viewmodel how to get it please can help me.
Regards
Srinivas