Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone,

I'm wrecking my brain in how to best handle getting this string through a MVVM pattern, which I'm quite new at. C# also isn't my strong point so it could be a very easy solution, I have no idea. English also isn't my first langue, but I'm trying my best.

What I'm trying to do is to get a string attached to a listbox selected item via my class, without ever specifying it in a element in my xaml.

I, for example, have several labels that show the details of a selected listbox item, like so :
Content="{Binding SelectedItem.UploadDate, ElementName=LBController}"

This works amazing and I'm happy with the result.
There's also another value stored inside the connected table with a string property. This property is used for functions within my application.

Ideally I would store this string inside the application outside of an element. There's really no need to let the user see it inside a label or textbox. I'd much rather that my viewmodel immediately picks up the selected listbox value, without ever storing it inside a label or such

I already have it defined inside my viewmodel, like so.

public string CurrentXMLListboxValue { get; set; }


But how do I get the value from my listbox? I dont want to store it(CurrentXMLListboxValue) inside of a label or textbox, I want it to immediately give the value without relying on such an item. Preferably through the listbox. Is this possible following MVVM structure?

What I have tried:

I honestly have no idea if what I want to do is possible. Storing the value inside a label or textbox only to have the viewmodel read that is not my preffered method of going about it, as I do not want the value to be visible. (Neither is an invisible label or textbox, for speed reasons).

My listbox does have a binding for a selected ID, but I'm not sure if that would be of any help.

<ListBox x:Name="LBController" Grid.Column="1" HorizontalAlignment="Left" Grid.RowSpan="6" Grid.Row="1"
         ItemsSource ="{Binding AllControllers, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding SelectedControllerID}"
         SelectedValuePath="Id" DisplayMemberPath="Name" >
</ListBox>
Posted
Updated 21-Jun-21 3:12am
v2
Comments
[no name] 20-Jun-21 11:52am    
With code-behind (oh horrors of horrors), you wouldn't have to "wreck your brain". And a "hidden" control is no big deal; particularly because it's the "rendering" that takes time (when doing "hundreds or thousands").
Kenneth Haugland 20-Jun-21 18:50pm    
Did you implement INotifyPropertyChanged on the properties in your ViewModel? How did you link up your ViewModel to the Window?

1 solution

Don't try to bind to the list's SelectedItem. Instead, bind to properties on your viewmodel.
C#
public IReadOnlyList<YourControllerClass> AllControllers { get; }

private int _selectedControllerId;

public int SelectedControllerId
{
    get { return _selectedControllerId; }
    set 
    { 
        if (_selectedControllerId == value) return;
        
        _selectedControllerId = value;
        OnPropertyChanged(nameof(SelectedController));
        OnPropertyChanged(nameof(UploadDate));
    }
}

public DateTime? UploadDate
{
    get
    {
        YourControllerClass selectedController = AllControllers.FirstOrDefault(c => c.Id == _selectedControllerId);
        if (selectedController == null) return null;
        return selectedController.UploadDate;
    }
}
XAML
<ListBox 
    x:Name="LBController" 
    ItemsSource="{Binding AllControllers, UpdateSourceTrigger=PropertyChanged}" 
    SelectedValue="{Binding SelectedControllerID}"
    SelectedValuePath="Id" DisplayMemberPath="Name" 
/>
<TextBlock
    Content="{Binding UploadDate}"
/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900