Good evening, please tell me there is a ListView:

<ListView x:Name="professions" MouseDoubleClick="professions_MouseDoubleClick"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock x:Name="id" Text="{Binding Key}"/> <TextBlock x:Name="val" Text="{Binding Value}"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> 

and there is an event handler:

 private void professions_MouseDoubleClick(object sender, MouseButtonEventArgs e) { groupSostav.Items.Add("???"); } 

With a double click, you need this (Text = "{Binding Value}") value to be added to the ListBox groupsostav, it is impossible to get it, what needs to be substituted into (groupSostav.Items.Add ("???");) instead of "? ?? " ?

In ListBox, entries are added from the constructor like this:

 Dictionary<int, string> col1 = new Dictionary<int, string>() { {1, "Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 1"}, {2, "Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 2"}, {3, "Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 3"} }; foreach (var profList in col1) { professions.Items.Add(new Item { Key = profList.Key, Value = profList.Value }); } } public class Item { public int Key { get; set; } public string Value { get; set; } } 
  • one
    Somehow you are working strangely: it seems that you are trying to use the buiding and at the same time fill out the lists in the coding on events. You already decide ... - Bulson

2 answers 2

If you are trying to work with the building, then here is an example for you, but without working in MVVM, as is customary.

XAML

 <Canvas> <ListView ItemsSource="{Binding ItemsForListView}" SelectedItem="{Binding SelectedItemInListView}" Height="100" Canvas.Left="24" Canvas.Top="33" Width="215" MouseDoubleClick="ListView_MouseDoubleClick"> <ListView.View> <GridView> <GridViewColumn Header="н/п" Width="30" DisplayMemberBinding="{Binding Key}"/> <GridViewColumn Header="НазваниС" Width="150" DisplayMemberBinding="{Binding Value}"/> </GridView> </ListView.View> </ListView> <ListBox ItemsSource="{Binding ItemsForListBox}" Height="180" Canvas.Left="323" Canvas.Top="40" Width="125"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Key}" Margin="5" /> <TextBlock Text="{Binding Value}" Margin="5" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Canvas> 

Codebehind

 public partial class MainWindow : Window, INotifyPropertyChanged { /// <summary> /// РСализация INotifyPropertyChanged /// </summary> public event PropertyChangedEventHandler PropertyChanged = delegate { }; /// <summary> /// ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ для ListView /// </summary> public ObservableCollection<Item> ItemsForListView { get; set; } /// <summary> /// Π­Π»Π΅ΠΌΠ΅Π½Ρ‚ Π²Ρ‹Π±Ρ€Π°Π½Π½Ρ‹ΠΉ Π² ListView /// </summary> private Item _SelectedItemInListView; public Item SelectedItemInListView { get { return _SelectedItemInListView; } set { _SelectedItemInListView = value; //сообщаСм ΠΎ Ρ‚ΠΎΠΌ, Ρ‡Ρ‚ΠΎ элСмСнт измСнился PropertyChanged(this, new PropertyChangedEventArgs(nameof(SelectedItemInListView))); } } /// <summary> /// ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ для ListBox /// </summary> public ObservableCollection<Item> ItemsForListBox { get; set; } public MainWindow() { InitializeComponent(); //подписываСмся Π½Π° событиС Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ ΠΎΠΊΠ½Π° this.Loaded += MainWindow_Loaded; } /// <summary> /// Π‘ΠΎΠ±Ρ‹Ρ‚ΠΈΠ΅ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ ΠΎΠΊΠ½Π° /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainWindow_Loaded(object sender, RoutedEventArgs e) { LoadLists(); //Π΄Π΅Π»Π°Π΅ΠΌ ΠΊΠΎΠ΄Π±ΠΈΡ…Π°ΠΉΠ½Π΄ источником Π΄Π°Π½Π½Ρ‹Ρ… для своСго XAML this.DataContext = this; } /// <summary> /// Π—Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ списков /// </summary> private void LoadLists() { ItemsForListView = new ObservableCollection<Item>() { new Item() { Key = 1, Value = "Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 1"}, new Item() { Key = 2, Value = "Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 2"}, new Item() { Key = 3, Value = "Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 3"} }; ItemsForListBox = new ObservableCollection<Item>(); } /// <summary> /// ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° события Π΄Π²ΠΎΠΉΠ½ΠΎΠ³ΠΎ ΠΊΠ»ΠΈΠΊΠ° ΠΌΡ‹ΡˆΡŒΡŽ Π½Π° ListView /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (SelectedItemInListView != null) { ItemsForListBox.Add(SelectedItemInListView); } } } public class Item { public int Key { get; set; } public string Value { get; set; } } } 
  • Thanks, sort of figured out, but how can I display the index of an item in the ItemsForListBox collection? - Dmitriy
  • @ Dmitry decided to create another int property to name it somehow "SelectedIndexInListBox" and bind it in ListBox SelectedIndex="{Binding SelectedIndexInListBox}" , and do not forget to set this property to have a change notification with pom PropertyChanged(this,...) , see how it was done on the SelectedItemInListView property - Bulson
  • @ Dmitriy then with this property "SelectedIndexInListBox" you can do whatever you want, bring it out - create a TextBlock and bind Text="{Binding SelectedIndexInListBox}" - Bulson

It turned out like this:

 private int _SelectedIndexInListBox; public int SelectedIndexInListBox { get { return _SelectedIndexInListBox; } set { _SelectedIndexInListBox = value; PropertyChanged(this, new PropertyChangedEventArgs(nameof(SelectedIndexInListBox))); } } 

And the conclusion:

 <GridViewColumn Header="β„–"DisplayMemberBinding="{Binding SelectedIndexInListBox}"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding SelectedIndexInListBox}"></TextBlock> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> 

It does not display anything, and if I create a separate TextBox (outside the ListView), then when I poke a mouse over the elements of the ListView to which I am attached, the value is output (the index of the element I chose).

I made two output options because I tested, none of the options work :(