'WPF C# get Object properties without adding extra variables into the viewmodel

I'm curently trying to implemnt a Combobox binding to a list and some labels showing the details of a selected object. The binding to the Combobox works without any problems but I don't want to add extra variables into my ViewModel just to display the name and speed of my car objects.

Is there any way to Map the object variables of the selected combobox item on to the labels without adding additional variables into my ViewModel?

That's the code I'm currently working with

MainWondow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StackOF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ViewModel viewModel;
        public MainWindow()
        {
            viewModel = new ViewModel();
            DataContext = viewModel;
            InitializeComponent();
        }
    }
}

ViewModel.cs

using _02_WPFCarInfo.AutoKlassen;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace StackOF
{
    public class ViewModel : INotifyPropertyChanged
    {

        private ObservableCollection<AutoModel> _autoModels;

        private AutoModel selectedModel;

        public ObservableCollection<AutoModel> AutoModels
        {
            get { return _autoModels; }
            set
            { 
                _autoModels = value;
                OnPropertyChanged();
            }
        }

        public AutoModel SelectedModel
        {
            get { return selectedModel; }
            set
            {
                selectedModel = value;
                OnPropertyChanged();
            }
        }


        public ViewModel()
        {

            _autoModels = generateCars();

        }



        private ObservableCollection<AutoModel> generateCars()
        {

            AutoModel model1 = new AutoModel();
            AutoModel model2 = new AutoModel();
            AutoModel model3 = new AutoModel();

            model1.name = "Mercedes C12 Pro";
            model1.speed = "125 km\\h";

            model2.name = "Audi A12";
            model2.speed = "236 km\\h";


            model3.name = "Audi S20 Pro";
            model3.speed = "300 km\\h";



            ObservableCollection<AutoModel> cars = new ObservableCollection<AutoModel>();

            cars.Add(model1);
            cars.Add(model2);
            cars.Add(model3);

            return cars;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

MainWindow.xaml

<Window x:Class="StackOF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StackOF"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ComboBox SelectedItem="{Binding SelectedModel}" DisplayMemberPath="name" ItemsSource="{Binding AutoModels}" HorizontalAlignment="Center" Margin="0,164,0,0" VerticalAlignment="Top" Width="212"/>
        <Label Content="Name:" HorizontalAlignment="Left" Margin="294,231,0,0" VerticalAlignment="Top"/>
        <Label Content="Speed:" HorizontalAlignment="Left" Margin="294,262,0,0" VerticalAlignment="Top"/>
        <TextBlock HorizontalAlignment="Center" Margin="0,241,0,0" TextWrapping="Wrap" Text="---" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>
        <TextBlock HorizontalAlignment="Center" Margin="0,267,0,0" TextWrapping="Wrap" Text="---" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>

    </Grid>
</Window>

AutoModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02_WPFCarInfo.AutoKlassen
{
    public class AutoModel
    {
        public String name { get; set; }
        public String speed { get; set; }

        public AutoModel()
        {
            this.speed = String.Empty;
            this.name = String.Empty;

        }
        public AutoModel(String name, String speed)
        {
            this.speed = speed;
            this.name = name;

        }
    }
}

I thought about something like :

<TextBlock HorizontalAlignment="Center" Margin="0,241,0,0" TextWrapping="Wrap" Text="{Binding SelectedModel.name}" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>
<TextBlock HorizontalAlignment="Center" Margin="0,267,0,0" TextWrapping="Wrap" Text="{Binding SelectedModel.speed}" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>

But obviously it is not possible to select the variables of "SelectedModel" that way.



Solution 1:[1]

Ok nvm it DOES work. It only does not work if you have a textblock with opening and closing tag with text inbetween

This works:

<TextBlock HorizontalAlignment="Center" Margin="0,241,0,0" TextWrapping="Wrap" Text="{Binding SelectedModel.name}" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>

but this doesn't

<TextBlock HorizontalAlignment="Center" Margin="0,241,0,0" TextWrapping="Wrap" Text="{Binding SelectedModel.name}" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303">---</TextBlock>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1