C
C#2y ago
edgarka_

❔ ❔ Bind selected items

In my MVVM, I have connected view and viewModel to display "Months" list, but not sure how to bind and send through selected ones. Could anyone help me with binding?
15 Replies
dancepanda42
dancepanda422y ago
Did you set the ViewModel to the DataContext of your View? like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
edgarka_
edgarka_OP2y ago
Yes -
dancepanda42
dancepanda422y ago
ok, the ItemsTemplate is unnecessary the listbox can manage text alone
edgarka_
edgarka_OP2y ago
They are populated, but I'd like to send selected to the model
dancepanda42
dancepanda422y ago
You can make a additional property like: public string SelectedItem {get; set:} and bind the Listbox propertye SelectedItem to it. Then the property in the ViewModel will have the value of the selection from the Listbox another hint, initialize your list in the constuctor. Then return the private field _month in the getter of the property. Instead of creating a new list each time.
edgarka_
edgarka_OP2y ago
Thank you @dancepanda42, I've added property and it grabs string properly Is it possible to send it as list, considering I have a multiple selection?
dancepanda42
dancepanda422y ago
yes, but this requires a little more code.
<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>
<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));
}
}
}
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));
}
}
}
To clarify. The SelectableItem gets the information that it is selected from the ListBoxItem through the binding in the style. To display the values, another property is now needed in the SelectableItem. This can also be displayed by an ItemTemplate via a binding. This example should work
edgarka_
edgarka_OP2y ago
I'll give it a go ObservableObject is part of some external package?
dancepanda42
dancepanda422y ago
yes its form the CommunityToolkit.Mvvm makes it more comfortable to write the mvvm pattern. but you can also use your ViewModelBase
edgarka_
edgarka_OP2y ago
Binding now works, thank you!
dancepanda42
dancepanda422y ago
your welcome
edgarka_
edgarka_OP2y ago
I'll try to play around for a bit, bind to models, would it be okay if I'll keep question open for some time and ask you if I'm stuck?
dancepanda42
dancepanda422y ago
sure am but in the Berlin time zone... so I'm no longer so long online. but you can send me a PM then I answer as soon as I find time.
edgarka_
edgarka_OP2y ago
Thank you @dancepanda42 🙂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server