C#
C#

help

Root Question Message

EdgarKa
EdgarKa11/23/2022
❔ ❔ 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?
dancepanda42
dancepanda4211/23/2022
Did you set the ViewModel to the DataContext of your View?
dancepanda42
dancepanda4211/23/2022
like this:
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
    }
EdgarKa
EdgarKa11/23/2022
Yes -
dancepanda42
dancepanda4211/23/2022
ok, the ItemsTemplate is unnecessary
the listbox can manage text alone
EdgarKa
EdgarKa11/23/2022
They are populated, but I'd like to send selected to the model
dancepanda42
dancepanda4211/23/2022
You can make a additional property like: public string SelectedItem {get; set:} and bind the Listbox propertye SelectedItem to it.
dancepanda42
dancepanda4211/23/2022
Then the property in the ViewModel will have the value of the selection from the Listbox
dancepanda42
dancepanda4211/23/2022
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
EdgarKa11/23/2022
Thank you @464427217787682816, I've added property and it grabs string properly
EdgarKa
EdgarKa11/23/2022
Is it possible to send it as list, considering I have a multiple selection?
dancepanda42
dancepanda4211/23/2022
yes, but this requires a little more code.
dancepanda42
dancepanda4211/23/2022
<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>
dancepanda42
dancepanda4211/23/2022
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));
        }
    }
}
dancepanda42
dancepanda4211/23/2022
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.
dancepanda42
dancepanda4211/23/2022
This example should work
EdgarKa
EdgarKa11/23/2022
I'll give it a go
EdgarKa
EdgarKa11/23/2022
ObservableObject is part of some external package?
dancepanda42
dancepanda4211/23/2022
yes its form the CommunityToolkit.Mvvm
dancepanda42
dancepanda4211/23/2022
makes it more comfortable to write the mvvm pattern.
dancepanda42
dancepanda4211/23/2022
but you can also use your ViewModelBase
EdgarKa
EdgarKa11/23/2022
Binding now works, thank you!
dancepanda42
dancepanda4211/23/2022
your welcome
EdgarKa
EdgarKa11/23/2022
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
dancepanda4211/23/2022
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
EdgarKa11/23/2022
Thank you @464427217787682816 🙂
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy