0

Hi 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);

        //dataquerry 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));
            }
        }
    }
}
  • I downvote because you didn't take the time to remove useless code and to fix the indentation. Please provide a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). And SO is [in english](https://meta.stackexchange.com/questions/13676/do-posts-have-to-be-in-english-on-stack-exchange/13684#13684). – Orace May 31 '22 at 12:28

1 Answers1

1

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.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Yes, I've seen it, but when I change "public ObservableCollection" to "public ObservableCollection" I have the problem at the db query in my ViewModel with " MeinInhalt = new ObservableCollection(Model.MeinInhalt.AlleMeinInhalte.Select(x => x.MeinInhalt).ToList());" .... its not working with "MeinInhalt = new ObservableCollection..." – LarsVegas100 May 31 '22 at 11:31
  • @LarsWichmann: That's because `Select(x => x.MeinInhalt)` extracts the strings out of our class - which is something you don't want to do if you want to keep the MeinInhalt type. `MeinInhalt = new ObservableCollection(Model.MeinInhalt.AlleMeinInhalte.ToList())` should suffice. – Heinzi May 31 '22 at 11:35
  • Again, you have overlooked this problem because `MeinInhalt` can mean x different things in your project, which makes it *extremely* hard to spot problems. Either just use a string (your MeinInhalt only has one property, so what's the point of it?) or give your stuff better names. Calling your ObservableCollection `MeineInhalte` instead of `MeinInhalt` would be a start, but don't stop there. :-) – Heinzi May 31 '22 at 11:37
  • yes / yupp it was the "Select(x => x.MeinInhalt)" and is working! Sorry about "or give your stuff better names. Calling your collection" ... it was a new named version from my work... but you solved it so quickly... i had already recognized the problem, but never thought that it would be the "select". Thank you I'm learning more and more – LarsVegas100 May 31 '22 at 11:49
  • @LarsWichmann: No need to apologize - war kein Vorwurf, nur ein gut gemeinter Tipp. :-) Glad to hear that it works now! – Heinzi May 31 '22 at 12:03