C# 访问子viewmodel的属性

C# 访问子viewmodel的属性,c#,wpf,mvvm,C#,Wpf,Mvvm,同样,我对WPF viewmodels的可见性或可访问性感到困惑。 我仍处于学习曲线的开始阶段,请耐心等待^^ “我的应用程序”是一个主窗口,带有一个菜单栏(功能区)和两个子用户控件,它们与自己的viewmodel一起加载。 一个子级包含settingsVM,另一个子级包含projectVM 这是两个子项及其内容控制的资源: <Window.Resources> <viewmodels:MainViewModel x:Key="MainVM" /&g

同样,我对WPF viewmodels的可见性或可访问性感到困惑。
我仍处于学习曲线的开始阶段,请耐心等待^^

“我的应用程序”是一个主窗口,带有一个菜单栏(功能区)和两个子用户控件,它们与自己的viewmodel一起加载。
一个子级包含settingsVM,另一个子级包含projectVM

这是两个子项及其内容控制的资源:

<Window.Resources>
    <viewmodels:MainViewModel x:Key="MainVM" />
    <DataTemplate x:Name="settingsViewTemplate" DataType="{x:Type viewmodels:SettingsViewModel}">
        <views:SettingsView DataContext="{Binding SettingsVM, Source={StaticResource MainVM}}"/>
    </DataTemplate>
    <DataTemplate x:Name="projectsViewTemplate" DataType="{x:Type viewmodels:ProjectViewModel}">
        <views:ProjectView DataContext="{Binding ProjectVM, Source={StaticResource MainVM}}"/>
    </DataTemplate>
</Window.Resources>

<!--.... a lot of menu stuff-->

<ContentControl Content="{Binding SelectedViewModel, Source={StaticResource MainVM}}"/>
现在我想在ribbonMenu中显示SettingsVM的一个属性。 如何从MainVM访问SettingsVM的属性

我尝试了以下几种方法:

<TextBox Text="{Binding SettingsVM.Masterfile, Mode=OneWay}" />

现在一切正常了:)

在DataTemplate中显式设置元素的DataContext通常是错误的。DataContext应该通过应用DataTemplate的ContentControl或ContentPresenter的属性值继承来传递。如果要将元素的DataContext设置为ContentControl的DataContext中对象的属性,则可能会出现异常,像
一样,首先像这样设置主窗口的数据上下文,这样就不必引用{Source={StaticResource MainVM}}并从资源中删除MainVM。由于某些原因,数据模板上有名称,请删除它们。你能给我们看一下你在菜单中使用的xaml吗?我怀疑你在不应该设置数据上下文的地方设置了数据上下文。好吧,恐怕我不理解这个概念。我想到了表单(控件包括它们的数据模型),我必须为每个“表单”定义一个数据模板。最后我展示了其中一种“形式”。也许有必要知道,设置和项目是完全分开的。设置是应用程序设置,如模板文件夹等,因为projectVM可以是独立于设置的每个项目。因此,我在启动时加载settingsVM,当加载项目时,我将设置作为参数传输到projectVMs Consturctor中。如果您按照我告诉您的那样设置数据上下文,则从绑定中删除
,Source={StaticResource MainVM}
。还要查看输出窗口并在其中查找错误,这就是您将发现绑定失败的原因。
<TextBox Text="{Binding SettingsVM.Masterfile, Mode=OneWay}" />
<TextBox Text="{Binding MainVM.SettingsVM.Masterfile, Mode=OneWay}" />
<ribbon:RibbonWindow
x:Class="PlcGenerator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clr-namespace:System.Windows.Controls.Ribbon;assembly=System.Windows.Controls.Ribbon"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyApp"
xmlns:controls="clr-namespace:MyApp.Views.Controls"
xmlns:misc="clr-namespace:MyApp.Misc"
xmlns:viewmodels="clr-namespace:MyApp.ViewModels"
xmlns:views="clr-namespace:MyApp.Views"
mc:Ignorable="d"
Closing="ClosingApp"
Title="My App" Height="700" Width="1200">


<Window.Resources>
    <DataTemplate x:Name="settingsViewTemplate" DataType="{x:Type viewmodels:SettingsViewModel}">
        <views:SettingsView DataContext="{Binding SettingsVM}"/>
    </DataTemplate>
    <DataTemplate x:Name="projectsViewTemplate" DataType="{x:Type viewmodels:ProjectViewModel}">
        <views:ProjectView DataContext="{Binding ProjectVM}"/>
    </DataTemplate>
</Window.Resources>


<DockPanel LastChildFill="True">
    
    <ribbon:Ribbon DockPanel.Dock="Top">
        <Ribbon.QuickAccessToolBar>
            <RibbonQuickAccessToolBar>
                <RibbonButton Label="Speichern" SmallImageSource="Icons/saveHS.png"/>
                
            </RibbonQuickAccessToolBar>
        </Ribbon.QuickAccessToolBar>

        <Ribbon.ApplicationMenu>
            <RibbonApplicationMenu SmallImageSource="Icons/ApplicationMenu.png">

                <RibbonApplicationMenuItem Header="Neues Projekt" ImageSource="Icons/32x32/Add_32x32.png" Command="{Binding ProjectVM.NewProject}" />
                <RibbonApplicationMenuItem Header="Projekt öffnen" ImageSource="Icons/32x32/Scan_32x32.png" Command="{Binding ProjectVM.OpenProject}" />
                <RibbonApplicationMenuItem Header="Projekt speichern" ImageSource="Icons/32x32/Save_32x32.png" Command="{Binding ProjectVM.SaveProject}" />
                <RibbonApplicationMenuItem Header="Beenden" ImageSource="Icons/32x32/Exit_32x32.png" Click="MnuExitClick" />
                <RibbonApplicationMenu.FooterPaneContent>
                    <RibbonButton Click="MnuExitClick" Label="Exit" SmallImageSource="Icons/32x32/Exit_32x32.png" HorizontalAlignment="Right"/>
                </RibbonApplicationMenu.FooterPaneContent>
           
            </RibbonApplicationMenu>
        </Ribbon.ApplicationMenu>

        <Ribbon.HelpPaneContent>
            <RibbonButton Content="Hilfe" SmallImageSource="Icons/32x32/Help_32x32.png"/>
        </Ribbon.HelpPaneContent>
        
        <!-- Start -->
        <RibbonTab x:Name="StartTab" Header="Start" >
            <RibbonGroup Header="Projekt">
                <RibbonButton Label="Projekt" Command="{Binding ProjectVM.ShowProject}" LargeImageSource="/Icons/32x32/ColorLayers_32x32.png" />
                <RibbonButton Label="Neues Projekt" SmallImageSource="Icons/32x32/Add_32x32.png" Command="{Binding ProjectVM.NewProject}"/>
                <RibbonButton Label="Projekt öffnen" SmallImageSource="Icons/32x32/Scan_32x32.png" Command="{Binding ProjectVM.OpenProject}"/>
                <RibbonButton Label="Speichern" SmallImageSource="Icons/32x32/Save_32x32.png" Command="{Binding ProjectVM.SaveProject}"/>


<TextBox Text="{Binding SettingsVM.MasterFile, Mode=OneWay}" />


            </RibbonGroup>


    </ribbon:Ribbon>

        <!-- Fensterinhalt-->
    <ContentControl Content="{Binding SelectedViewModel}"/>
    <!--</Grid>-->
</DockPanel>

</ribbon:RibbonWindow>
<Window.Resources>
    <DataTemplate DataType="{x:Type viewmodels:SettingsViewModel}">
        <views:SettingsView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type viewmodels:ProjectViewModel}">
        <views:ProjectView />
    </DataTemplate>
</Window.Resources>