Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / WPF - ComboBox / 2 сообщений из 2, страница 1 из 1
30.03.2009, 18:13
    #35901252
SomeQuestion
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
WPF - ComboBox
Как забиндить комманду к ComboBoxItem? То есть чтобы по выбору какого-то айтема комбобокса инициировалась комманда...
...
Рейтинг: 0 / 0
30.03.2009, 19:58
    #35901448
SeVa
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
WPF - ComboBox
Это можно сделать через Attached Behavior
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
public class SelectionBehavior
{
    public static DependencyProperty SelectionChangedProperty =
            DependencyProperty.RegisterAttached("SelectionChanged",
            typeof(ICommand),
            typeof(SelectionBehavior),
            new UIPropertyMetadata(SelectionBehavior.SelectedItemChanged));
    public static void SetSelectionChanged(DependencyObject target, ICommand value)
    {
        target.SetValue(SelectionBehavior.SelectionChangedProperty, value);
    }
    private static void SelectedItemChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        Selector element = target as Selector;
        if (element == null) throw new InvalidOperationException("This behavior can be attached to Selector item only.");
        if ((e.NewValue != null) && (e.OldValue == null))
        {
            element.SelectionChanged += SelectionChanged;
        }
        else if ((e.NewValue == null) && (e.OldValue != null))
        {
            element.SelectionChanged -= SelectionChanged;
        }
    }
    private static void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        UIElement element = (UIElement)sender;
        ICommand command = (ICommand)element.GetValue(SelectionBehavior.SelectionChangedProperty);
        command.Execute(((Selector) sender).SelectedValue);
    }
}

Код: plaintext
1.
2.
3.
<ComboBox local:SelectionBehavior.SelectionChanged="{Binding MyCommand}"  
              ItemsSource="{Binding MySource}"
              Name="MyComboBox"/> 

MyCommand - DelegateCommand из CompositeWPF
...
Рейтинг: 0 / 0
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / WPF - ComboBox / 2 сообщений из 2, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]