help
Root Question Message
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
public string SelectedItem {get; set:}
and bind the Listbox propertye SelectedItem to it.<ListBox ItemsSource="{Binding Items}" SelectionMode="Multiple" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayText}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected"
Value="{Binding IsSelected}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
public partial class MainWindowViewModel : ObservableObject
{
private List<SelectableItem> _items;
public List<SelectableItem> Items
{
get => _items;
set
{
_items = value;
OnPropertyChanged(nameof(Items));
}
}
public MainWindowViewModel()
{
_items = new List<SelectableItem>
{
new SelectableItem { DisplayText = "Entry01"},
new SelectableItem { DisplayText = "Entry02"},
new SelectableItem { DisplayText = "Entry03"},
new SelectableItem { DisplayText = "Entry04"},
};
}
}
public class SelectableItem : ObservableObject
{
private string _displayText;
public string DisplayText
{
get => _displayText;
set
{
_displayText = value;
OnPropertyChanged(nameof(_displayText));
}
}
private bool _isSelected;
public bool IsSelected
{
get => _isSelected;
set
{
_isSelected = value;
OnPropertyChanged(nameof(_isSelected));
}
}
}