Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
We have one silverlight application with MVVM Architecture.

in that i have one User Control (SearchUC.xaml) for binding combobox items inline. like below:
HTML
<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"&gt;
    
    <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

C#
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
HTML
<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"&gt;
    <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" />               &lt;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}" /&gt;
        
    </grid>
</usercontrol>

And View model (MainPageViewModel.cs) as an below
C#
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
        /// <summary>
        /// Notifies when properties are changed  
        /// </summary>
        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
Posted
Updated 15-Jan-13 7:30am
v2

1 solution

Ok, you will need to follow the event aggregator / messenger / mediator pattern.

I really would recommend Prism or Jounce or Cinch for any Silverlight / WPF stuff (Jounce does not support WPF)

http://cinch.codeplex.com/[^]
http://compositewpf.codeplex.com/[^]
http://jounce.codeplex.com/[^]

In essence, your control registers an event (the dropdown selected and or button click). Other controls will subscribe to this event and any action you take will update the other controls using a publish / subscribe pattern.

So, I use Prism and with Prism there is something called the EventAggregator. What you would do is register the button click with this global object in the SearchUC control. You would then subscribe to this global (well singleton) and listen for events that you can then do stuff with.

I can't stress how important it is to implement a framework that handles many of the boilerplate code you are writing (a lot).

Event aggregator example
[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900