LarsVegas100
LarsVegas100

Reputation: 59

C# - Return the Value from a Combobox with C# MVVM

I don't know what I forgot to return the value of the combo box. I'm already doing the fifth round and can't find the error.

The data is correctly loaded from the DB (model) and displayed in the combo box (viewModel). When I select an element, I can't just output it via console or something.

XAML:

<Label Grid.Row="0" Margin="0" FontSize="45" Content="{Binding LabelText}" HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Column="1" Padding="5,1,5,5"/>
<ComboBox Grid.Row="0" Margin="83.2,98,82.8,-111.2" ItemsSource="{Binding MeinInhalt, NotifyOnSourceUpdated=True}" SelectedValue="{Binding AusgewaehlterInhalt, NotifyOnSourceUpdated=True}" FontSize="48" Background="LightBlue" MaxDropDownHeight="850" Width="888" Height="95" VerticalAlignment="Top" Grid.Column="1" Padding="5,1">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding CommandDDLChanged}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

C# ViewModel/TestVM.cs:

class TestVM : MainVM
{
    private MeinInhalt _ausgewaehlterInhalt;
    private RelayCommand<object> _commandDDLChanged;

    public ICommand UpdateViewCommand { get; set; }

    public string LabelText
    {
        get 
        {
            return _labelText; 
        }
        set
        {
            _labelText = value;
        }
    }

    //zeigt alle MeinInhalte an
    public ObservableCollection<string> MeinInhalt { get; }

    //zeigt alle MeinInhalte zu beginn, bei auswahl aendert sich aber nichts
    public MeinInhalt AusgewaehlterInhalt
    {
        get { return _ausgewaehlterInhalt; }
        set
        {
            if (_ausgewaehlterInhalt == null)
            {
                _ausgewaehlterInhalt = value;
                Console.WriteLine("Der Ausgewaehlte MeinInhalt a) ist: {0}", _ausgewaehlterInhalt);
            }
            else
            {
                _ausgewaehlterInhalt = value;
                Console.WriteLine("Der Ausgewaehlte MeinInhalt b) ist: {0}", _ausgewaehlterInhalt);

            }

            OnPropertyChanged("AusgwaehlterMeinInhalt");
        }
    }


    public object _meinInhaltErfassen { get; private set; }

    public TestVM(MainWindowVM mainwinVM, string SELECTION)
    {
        UpdateViewCommand = new UpdateViewCommand(mainwinVM);

        // data query is working and shown in combobox
        MeinInhalt = new ObservableCollection<string>(Model.MeinInhalt.AlleMeinInhalte.Select(x => x.MeinInhalt).ToList());

    }

    public RelayCommand<object> CommandDDLChanged
    {
        get
        {
            if (_commandDDLChanged == null)
            {
                _commandDDLChanged = new RelayCommand<object>(execute => DoCommandDDLChanged());
            }
            return _commandDDLChanged;
        }
    }

    private void DoCommandDDLChanged()
    {
        neuelisteMeinInhalte();
        Console.WriteLine("Der Ausgewaehlte MeinInhalt c) ist: {0}", _ausgewaehlterInhalt);
    }

    public void neuelisteMeinInhalte()
    {
        Console.WriteLine("Der Ausgewaehlte MeinInhalt d) ist: {0}", _ausgewaehlterInhalt);
        
        OnPropertyChanged("FreieFrachtbreife");
    }
}

c# Model/MeinInhalt.cs

public class MeinInhalt : tblTest
{

    public MeinInhalt(string meinInhalt)
    {
        MeinInhalt = meinInhalt;
    }

    public static List<MeinInhalt> AlleMeinInhalte { get;private set; }
    public static List<MeinInhalt> VerwendeterMeinInhalt { get; private set; }

    public static void initialisiereMeinInhalt()
    {
        AlleMeinInhalte = new List<MeinInhalt>();
        VerwendeterMeinInhalt = new List<MeinInhalt>();

        using (var db = new SomeEntities())
        {

            var query = db.tblTest.Select(x => new { x.MeinInhalt }).Distinct().ToList();

            foreach (var item in query)
            {
                AlleMeinInhalte.Add(new MeinInhalt(item.MeinInhalt));
            }
        }
    }
}

Upvotes: 0

Views: 59

Answers (1)

Heinzi
Heinzi

Reputation: 172418

Your data source is a collection of strings:

<ComboBox ... ItemsSource="{Binding MeinInhalt, NotifyOnSourceUpdated=True}" ... />

public ObservableCollection<string> MeinInhalt { get; }

Thus, once an element is selected, the selected value is a string.

However, the SelectedValue property of your combobox is bound to a ViewModel property of type MeinInhalt. You can't assign a value of type string to a property of type MeinInhalt¹. Thus, the binding fails.


Side note: When a binding fails, you get an (easy to miss) error message in your output window. You might want to consider activating a BindingErrorListener in your debug builds, so that Visual Studio will actively notify you of such issues.


Footnotes:

¹ Calling both your custom type and a property of a different type (string) MeinInhalt is confusing. Don't do that. If a property has the same name as a type, I expect it to be of that type.

Upvotes: 1

Related Questions