powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / WPF - ComboBox
2 сообщений из 2, страница 1 из 1
WPF - ComboBox
    #35901252
SomeQuestion
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Как забиндить комманду к ComboBoxItem? То есть чтобы по выбору какого-то айтема комбобокса инициировалась комманда...
...
Рейтинг: 0 / 0
WPF - ComboBox
    #35901448
SeVa
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Это можно сделать через 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
2 сообщений из 2, страница 1 из 1
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / WPF - ComboBox
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


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