How to: Data Binding for ListPicker.SelectedItems

ListPicker.SelectedItems is a “IList” Property, so you must prepare the property typed “IList”, in your ViewModel.

Here is sample implementation::

in XAML:

        <toolkit:ListPicker Name="AccountListPicker"
                            Grid.ColumnSpan="2"
                            Margin="12,8"
                            ItemsSource="{Binding AuthInfos,
                                                  Mode=OneWay}"
                            SelectedItems="{Binding Selected,
                                                    Mode=TwoWay}"
                            SelectionMode="Multiple"
                            d:LayoutOverrides="Height" />

in ViewModel:

        private ObservableCollection _selected = new ObservableCollection();
        public IList Selected
        {
            get { return _selected; }
            set
            {
                if (_selected == value) return;
                _selected = new ObservableCollection();
                value.OfType().ForEach(a => _selected.Add(a));
                RaisePropertyChanged(() => Selected);
            }
        }

Additional: You should make sure that Collection have freezed which is binding in ItemsSource.

bad sample:

public IEnumerable ItemsSource
{
	// New instances are created every calling ItemsSource.
	get { return SomeModel.Query(); }
}

good sample:

private IEnumerable _itemsSource = SomeModel.Query().ToArray();
public IEnumerable ItemsSource
{
	get { return _itemsSource; }
}

Have fun with Silverlight WP! XD

MVVMでVMからCodeBehindに通知するとき

覚え書き。

ViewModelからCodeBehindへ通知するとき、つまりは通常バインドで解決されるようなルートにおいてCodeBehindのメソッドを呼び出したいとき。

VM(メッセージをRaise)→[メッセージ]→View(メッセージトリガ)→[CallMethodAction]→CodeBehind

とすれば動いてくれる。

単にCodeBehindにメソッド定義しておけばそれを呼べるので一番シンプル?