Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Silverlight MVVM
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"&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
 
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"&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
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 15-Jan-13 3:03am
Edited 15-Jan-13 8:30am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
[^]
  Permalink  

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

  Print Answers RSS
0 DamithSL 245
1 Sergey Alexandrovich Kryukov 217
2 OriginalGriff 200
3 Peter Leow 160
4 Afzaal Ahmad Zeeshan 85
0 OriginalGriff 7,380
1 DamithSL 5,359
2 Sergey Alexandrovich Kryukov 4,944
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100