Laden...

Binding an darüberliegendem DataContext?

Erstellt von grobekelle vor 2 Jahren Letzter Beitrag vor 2 Jahren 608 Views
G
grobekelle Themenstarter:in
4 Beiträge seit 2018
vor 2 Jahren
Binding an darüberliegendem DataContext?

Guten Tag,

alles was ich über das Programmieren weiß, habe ich mir durch Online-Trainings und Recherche selber beigebracht, daher kann meine Ausdrucksweise an der ein oder anderen Stelle unverständlich wirken, ich hoffe jedoch, das man versteht was ich meine.

Zu meinem Problem:

Vorab ich verwende das MVVM Pattern.

Ich habe ein UserControl in dem ein ContentControl Element und zwei Buttons vorhanden sind, die den Inhalt des ContentControl steuern.
Diese View sieht wie folgt aus.


<UserControl x:Class="GC_Log_Config.Views.DeviceView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:GC_Log_Config.Views"
             mc:Ignorable="d" 
             Background="White"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="29"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Width="120" Margin="2,4,2,4" Command="{Binding LoadAddDeviceVMCommand}">Add Device</Button>
            <Button Width="120" Margin="2,4,2,4">Remove Device</Button>
        </StackPanel>
        <ContentControl Content="{Binding DevicesView}" Grid.Row="0"/>
    </Grid>
</UserControl>

Und das dazugehörige ViewModel wie folgt:


    public class DeviceViewModel : ObservableObject
    {
        private JsonDeviceDataService _deviceData = new JsonDeviceDataService();
        private List<DeviceModel> _devices;

        private object _devicesView;
        public object DevicesView
        {
            get { return _devicesView; }
            set { OnPropertyChanged(ref _devicesView, value); }
        }
        public List<DeviceModel> Devices
        {
            get { return _devices; }
            set { OnPropertyChanged(ref _devices, value); }
        }

        public ICommand LoadAddDeviceVMCommand { get; private set; }
        public ICommand LoadDeviceListVMCommand { get; private set; }

        public DeviceViewModel()
        {
            LoadAddDeviceVMCommand = new RelayCommand(LoadAddDeviceVM);
            LoadDeviceListVMCommand = new RelayCommand(LoadDeviceListVM);
            DevicesView = new DevicesListViewModel();
            Devices = _deviceData.LoadDevices();
        }

        private void LoadDeviceListVM()
        {
            DevicesView = new DevicesListViewModel();
        }

        private void LoadAddDeviceVM()
        { 
            DevicesView = new AddDeviceViewModel();
        }
    }

Eines der UserControls die in dem ContentControl geladen wird, hat einen Button, mit dem ich den DataContext DevicesView gern wieder ändern möchte,
um zum Beispiel wieder das ViewModel zu laden, welches im Konstruktor steht. Vereinfacht gesagt, ich möchte den Button an das Command LoadDeviceListVMCommand binden,
welches sich aber in dem DeviceViewModel befindet. Nun weiß ich nicht wie ich das bewerkstelligen soll. Ich hoffe es ist einigermaßen verständlich und jemand kann mir sagen wie ich das Binding machen kann.