简体   繁体   中英

c# MVVM: How to bind 2 ContentControls to 2 Properties of MainViewModel

I'm working on a daily task manager.

I want to display 2 different views inside of 2 different ContentControls in my MainWindow.xaml. I have a ViewModelMain, which contains two properties of type ViewModelBase. So I can switch between different ViewModels, because they all implement the ViewModelBase, which implements INotifyPropertyChanged. With just one ContentControl it is no Problem to choose any view. In fact it is possible (when configured in the Command) to select the viewmodels from the second ContentControl correctly.

Now I need help... why is my second ContentControl not displaying anything? I've tried to evaluate, where the problem is, but the ViewModel selection is passed as expected and the OnPropertyChanged Method is called.

Thanks for your help.

My ViewModelMain:

public class ViewModelMain : ViewModelBase {

        private ViewModelBase selectedVMMain;
        private ViewModelBase selectedVMEssentail;


        public ViewModelBase SelectedVMMain {
            get { return selectedVMMain; }
            set {
                if( selectedVMMain != value ) {
                    selectedVMMain = value;
                    OnPropertyChanged(nameof(selectedVMMain));
                }
            }
        }
        public ViewModelBase SelectedVMEssential {
            get { return selectedVMEssentail; }
            set {
                if( selectedVMEssentail != value ) {
                    selectedVMEssentail = value;
                    OnPropertyChanged(nameof(selectedVMEssentail));
                }
            }
        }

        public ICommand CommandUpdateView { get; set; }

        public ViewModelMain() {
            this.CommandUpdateView = new CommandUpdateView(this);
        }

}

CommandUpdateView:

private ViewModelMain viewModelMain;
        public event EventHandler CanExecuteChanged;

        public CommandUpdateView( ViewModelMain _viewModelMain) {
            this.viewModelMain = _viewModelMain;
        }

        public bool CanExecute( object parameter ) => true;
        public void Execute( object parameter ) {
            switch( parameter ) {

            case nameof(EnumViewModels.Übersicht):
                viewModelMain.SelectedVMMain = new ViewModelÜbersicht();
                break;
            case nameof(EnumViewModels.Statistik):
                viewModelMain.SelectedVMMain = new ViewModelStatistik();
                break;

            case nameof(EnumViewModels.Kategorie):
                viewModelMain.SelectedVMEssential = new ViewModelKategorie();
                break;
            case nameof(EnumViewModels.Projekt):
                viewModelMain.SelectedVMEssential = new ViewModelProjekt();
                break;
            case nameof(EnumViewModels.Aufgabe):
                viewModelMain.SelectedVMEssential = new ViewModelAufgabe();
                break;
            case nameof(EnumViewModels.Pomodoro):
                viewModelMain.SelectedVMEssential = new ViewModelPomodoro();
                break;

            default:
                viewModelMain.SelectedVMMain = new ViewModelTEST();
                viewModelMain.SelectedVMEssential = new ViewModelTEST();
                break;
            }
        }
    }

MainWindow.xaml (DataContext is set to new ViewModelMain)

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="100" Width="Auto"/>
            <ColumnDefinition MinWidth="200" Width="Auto"/>
            <ColumnDefinition MinWidth="200" Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Top" 
                    Grid.Column="0" Grid.Row="0"
                    Margin="5" Width="100" >

            <Button x:Name="Button_Uebersicht" Content="Übersicht"
                    Command="{Binding CommandUpdateView}" CommandParameter="Übersicht"
                    HorizontalAlignment="Stretch" VerticalAlignment="Center" 
                    Margin="0,5" />
            <Button x:Name="Button_Statistik" Content="Statistik" 
                    Command="{Binding CommandUpdateView}" CommandParameter="Statistik"
                    HorizontalAlignment="Stretch" VerticalAlignment="Center"
                    Margin="0,5" />
            <Button x:Name="Button_Kategorie" Content="Kategorie" 
                    Command="{Binding CommandUpdateView}" CommandParameter="Kategorie"
                    HorizontalAlignment="Stretch" VerticalAlignment="Center"
                    Margin="0,5" />
            <Button x:Name="Button_Projekt" Content="Projekt" 
                    Command="{Binding CommandUpdateView}" CommandParameter="Projekt"
                    HorizontalAlignment="Stretch" VerticalAlignment="Center"
                    Margin="0,5" />
            <Button x:Name="Button_Aufgabe" Content="Aufgabe" 
                    Command="{Binding CommandUpdateView}" CommandParameter="Aufgabe"
                    HorizontalAlignment="Stretch" VerticalAlignment="Center"
                    Margin="0,5" />
            <Button x:Name="Button_Pomodoro" Content="Pomodoro" 
                    Command="{Binding CommandUpdateView}" CommandParameter="Pomodoro"
                    HorizontalAlignment="Stretch" VerticalAlignment="Center"
                    Margin="0,5" />

            <Button x:Name="Button_TEST" Content="TEST" 
                    Command="{Binding CommandUpdateView}"
                    HorizontalAlignment="Stretch" VerticalAlignment="Center"
                    Margin="0,5" />

        </StackPanel>

        <ContentControl x:Name="CC_Main"
                        Content="{Binding SelectedVMMain}"
                        Grid.Column="1" Grid.Row="0"
                        Margin="5" Width="Auto"/>
        <ContentControl x:Name="CC_Essential"
                        Content="{Binding SelectedVMEssential}"
                        Grid.Column="2" Grid.Row="0"
                        Margin="5" Width="Auto"/>

SOLVED

It was the field, which was misspelled... the Property was named correctly, but the the Binding didn't work because, i used the nameof(field) keyword...

Thanks for the nice introduction on Stack Overflow!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM