locked
How To: Prism Commanding with Silverlight Combobox RRS feed

  • Question

  • So I'm needing to use a combobox with prism commands after some effort I realized that the silverlight combobox control does not implement ICommandSource and therefore needs to be updated for it to work with commanding and prism. However silverlight does not have a ICommandSource interface. I've seen some work arounds on the net for this issue, but I was wondering what else you guys may have seen or tried.

    Tuesday, April 27, 2010 12:49 PM

Answers

  • I found something here:

    http://openlightgroup.net/Blog/tabid/58/EntryId/82/Blend-4-TreeView-SelectedItemChanged-using-MVVM.aspx

    Regards,

    Kasimier

    Wednesday, April 28, 2010 12:32 PM

All replies

  • Well I did see this one already and I'm not getting how it could work. As I understand it a silverlight combobox does not implement the ICommandSource interface which is necessary to send commands. So yes I know how to implement the CommandBehaviorBase object but I'm not following how this helps me overcome the lack of ICommandSource in silverlight combobox.

    Tuesday, April 27, 2010 1:19 PM
  • So what I'm trying to do is use the selected item as a parameter to do some logic. I think this is the disconnect. Now the problem is that there is no ICommandSource and IInputElement in silverlight. So I'm interested in discovering what others have done to get around this.

    Tuesday, April 27, 2010 2:59 PM
  • So I'm interested in discovering what others have done to get around this.
     

    Well, I gave up in this specific case.

    When the bunch of items used by the ComboBox sits directly on the ViewModel, then one could expose a PagedCollectionView on the ViewModel and react on the changes of the SelectedItem on that PagedCollectionView.

    If the scenario gets more complicated, then I resort to using event handlers in the code behind which propagate the stuff via the ViewModel interface to the ViewModel.

    But that's just me in this case. Please don't stop poking. Things don't get better if people give up :-)

    Regards,

    Kasimier

     

    Tuesday, April 27, 2010 3:37 PM
  • I found something here:

    http://openlightgroup.net/Blog/tabid/58/EntryId/82/Blend-4-TreeView-SelectedItemChanged-using-MVVM.aspx

    Regards,

    Kasimier

    Wednesday, April 28, 2010 12:32 PM
  • Example

    Assemblies:
    Microsoft.Practices.Composite
    Microsoft.Practices.Composite.Presentation
    System.Windows.Interactivity

    Externals:
    http://goodiesforria.svn.sourceforge.net/viewvc/goodiesforria/trunk/Casimodo/Casimodo.Lib.Silverlight/ComponentModel/ObservableObject.cs

    <UserControl
        x:Class="SelectedItem01.MainPage"
        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:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:local="clr-namespace:SelectedItem01"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        <UserControl.Resources>
            <local:MyViewModel
                x:Key="ViewModel" />
        </UserControl.Resources>

        <Grid
            x:Name="LayoutRoot"
            Background="White"
            DataContext="{StaticResource ViewModel}">
            <StackPanel>
                <TextBlock
                    Text="{Binding SelectedItemName}"
                    Height="30"
                    Margin="5" />
                <ComboBox
                    x:Name="myComboBox"
                    Height="30"
                    Margin="5"
                    ItemsSource="{Binding Items}"
                    DisplayMemberPath="Name">
                    <i:Interaction.Triggers>
                        <i:EventTrigger
                            EventName="SelectionChanged">
                            <i:InvokeCommandAction
                                Command="{Binding DoSomethingCommand, Source={StaticResource ViewModel}}"
                                CommandParameter="{Binding SelectedItem, ElementName=myComboBox, Mode=OneWay}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </ComboBox>
            </StackPanel>
        </Grid>
    </UserControl>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Collections.ObjectModel;
    using Microsoft.Practices.Composite.Presentation.Commands;
    using System.ComponentModel;
    using Casimodo.Lib.ComponentModel;

    namespace SelectedItem01
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }
        }
        
        public class MyViewModel : ObservableObject
        {
            public MyViewModel()
            {
                this.DoSomethingCommand = new DelegateCommand<object>(a => DoWithSelectedItem(a));
                this.Items = new ObservableCollection<MyDataItem>();
                this.Items.Add(new MyDataItem());
                this.Items.Add(new MyDataItem());
            }

            void DoWithSelectedItem(object obj)
            {
                MyDataItem item = obj as MyDataItem;
                if (item == null)
                    return;

                this.SelectedItemName = item.Name;
            }

            public string SelectedItemName
            {
                get { return _selectedItemName; }
                set { SetProperty(SelectedItemNameChangedArgs, ref _selectedItemName, value); }
            }
            string _selectedItemName;
            public static readonly PropertyChangedEventArgs SelectedItemNameChangedArgs = new PropertyChangedEventArgs("SelectedItemName");

            public ICommand DoSomethingCommand { get; private set; }

            public ObservableCollection<MyDataItem> Items { get; private set; }        
        }

        public class MyDataItem
        {
            static int counter;

            public MyDataItem()
            {
                counter++;
                this.Name = "Item no. " + counter.ToString();
            }

            public string Name { get; set; }        
        }
    }

    Regards,

    Kasimier

    Wednesday, April 28, 2010 3:20 PM