There is a list

<ListBox x:Name="listboxFolder" BorderThickness="0" ItemsSource="{Binding Path=ClientsOnlineList, UpdateSourceTrigger=PropertyChanged}" Margin="0,50,0,10" HorizontalAlignment="Left" Width="301" Background="#FFD0E6C5" > <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="SelectClient"></EventSetter> <Setter Property="Margin" Value="5"/> <Setter Property="Padding" Value="5"/> <Setter Property="Cursor" Value="Hand"/> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Foreground" Value="Green"/>Bold"/> <Setter Property="FontSize" Value="23"/> </Trigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate > <DataTemplate > <TextBlock Text="{Binding UserName}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

ClientsOnlineList - List. Contains the fields UserName, id, etc.

How can I find out when selecting a list- which ClientsOnlineList.Id was selected? Since the list is stylized, I was lost in the links and how to find out the selected item?

    2 answers 2

    Create a property (type you yourself know) CurrentClient and bind it to the SelectedItem selection in the ListBox will be available through this property.

      /// <summary> /// Это коллекция для заполнения ListBox /// </summary> private ObservableCollection<Animal> _Animals; public ObservableCollection<Animal> Animals { get { return _Animals; } set { if (_Animals == value) return; _Animals = value; PropertyChanged(this, new PropertyChangedEventArgs(nameof(Animals))); } } /// <summary> /// Текущее выбранное животное в ListBox /// </summary> private Animal _CurrentAnimal; public Animal CurrentAnimal { get { return _CurrentAnimal; } set { if (_CurrentAnimal == value) return; _CurrentAnimal = value; PropertyChanged(this, new PropertyChangedEventArgs(nameof(CurrentAnimal))); } } 

    In xaml

      <ListBox ItemsSource="{Binding Animals, Mode=TwoWay}" SelectedItem="{Binding CurrentAnimal}"/> 
    • And you can write in more detail? - Rakzin Roman
    • Changed the answer. A full example can download - Bulson

    It's simple. Here is a handler for your eventsetter:

     private void SelectClient(object sender, MouseEventArgs e) { var index = listboxFolder.Items.IndexOf(((ListBoxItem)sender).DataContext); MessageBox.Show(index.ToString()); } 

    It will return the position of the element relative to the sheet itself. In principle, this is the same listboxFolder.SelectedIndex, but you can optimize it for yourself.

    But since you need an id, use the following code:

     listboxFolder.SelectionChanged += (sender, e) => { var id = (listboxFolder.SelectedItem as [Ваш класс клиента]).id; MessageBox.Show(id.ToString()); }; 

    And there is nothing to write extra code in the data model.

    • one
      The author of the question uses biding (this is evident from the XAML markup), i.e. works in the paradigm of the MVVM pattern. And your example is from a simple approach to working with event handlers in Kodbihind, which is good. - Bulson
    • @Bulson, this is one of the solutions. - Fortune
    • @Bulson, but the second should please you. Re-read my answer. - Fortune
    • what should i like? In principle, yes, you can work in a small training project, but you don’t need it if you work on a serious application. - Bulson
    • Above, I gave a link to a small example project on how to work with collections in WPF, you can also download and watch. - Bulson