powered by simpleCommunicator - 2.0.51     © 2025 Programmizd 02
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Биндинг Combobox в зависимости от другого Combobox
3 сообщений из 3, страница 1 из 1
Биндинг Combobox в зависимости от другого Combobox
    #37396480
Dmitry V. Liseev
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Есть некоторый класс с двумя уровнями иерархии данных:
Код: 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.
public class FirstList
    {
        private Dictionary<string, List<string>> m_list2 = new Dictionary<string,List<string>>();

        public FirstList()
        {
            List<string> item;

            item = new List<string>();
            m_list2.Add("aaa", item);
            item.Add("aaa 1");
            item.Add("aaa 2");
            item = new List<string>();
            m_list2.Add("bbb", item);
            item.Add("bbb 1");
            item.Add("bbb 2");
        }

        public IEnumerable<string> List1
        {
            get { return m_list2.Keys; }
        }

        public List<string> List2(string index)
        {
            return m_list2[index];
        }
    }

И есть два Combobox. Один отображает первый уровень иерархии. Как сделать, чтобы другой отображал второй уровень в зависимости от выбранного первого?
Код: plaintext
1.
2.
3.
4.
5.
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,36,0,0"
        Name="comboBox1" VerticalAlignment="Top" Width="198"
              ItemsSource="{Binding Path=List1}"/>
    <ComboBox Height="23" HorizontalAlignment="Right" Margin="0,36,55,0"
        Name="comboBox2" VerticalAlignment="Top" Width="232" />
...
Рейтинг: 0 / 0
Биндинг Combobox в зависимости от другого Combobox
    #37396574
koJIo6ok
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
а если уровней станет больше? что тогда? комбобокс добавлять на каждый уровень?
мб пригодится
...
Рейтинг: 0 / 0
Биндинг Combobox в зависимости от другого Combobox
    #37396606
Dmitry V. Liseev
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
koJIo6okа если уровней станет больше?Гарантированно не станет.

Все, разобрался:
Код: 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.
32.
33.
34.
35.
36.
    public class FirstList
    {
        private ObservableCollection<ListItem> m_roots
            = new ObservableCollection<ListItem>();

        public FirstList()
        {
            ListItem item;

            item = new ListItem { ItemName = "aaa" };
            m_roots.Add(item);
            item.Childrens.Add("aaa 1");
            item.Childrens.Add("aaa 2");
            item = new ListItem { ItemName = "bbb" };
            m_roots.Add(item);
            item.Childrens.Add("bbb 1");
            item.Childrens.Add("bbb 2");
        }

        public ObservableCollection<ListItem> Roots
        {
            get { return m_roots; }
        }
    }

    public class ListItem
    {
        private ObservableCollection<string> m_childrens
            = new ObservableCollection<string>();

        public string ItemName { get; set; }

        public ObservableCollection<string> Childrens
        {
            get { return m_childrens; }
        }
    }
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
    <ComboBox
        x:Name="comboBox1"
        HorizontalAlignment="Left" Margin="12,36,0,0"
        VerticalAlignment="Top" Width="198"
        ItemsSource="{Binding Path=Roots}"
        SelectedIndex="0">
      <ComboBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding ItemName}" />
        </DataTemplate>
      </ComboBox.ItemTemplate>
    </ComboBox>
    <ComboBox
        HorizontalAlignment="Left" Margin="216,36,0,0"
        VerticalAlignment="Top" Width="232"
        ItemsSource="{Binding ElementName=comboBox1, Path=SelectedItem.Childrens}"
        SelectedIndex="0" />
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Биндинг Combobox в зависимости от другого Combobox
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


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