我和我的朋友有一个主窗口,有两个网格行,每行应该代表不同的视图。顶视图是我们希望根据您拥有的授权级别进行交换的导航栏,因此能够访问不同的内容。
我们在名为OperatorNavView的另一个名称空间中有一个视图,其代码如下所示
<Grid>
<StackPanel x:Name="NavBar" Orientation="Horizontal" Height="75" VerticalAlignment="Top" HorizontalAlignment="Stretch" Background="White" Grid.Row="0">
<Button x:Name="btnTeams" Content="Lag" Command="{Binding NavigationCommand}" CommandParameter="Team" HorizontalAlignment="Left" Width="77" VerticalAlignment="Top" Background="#d87900" Height="44" />
<Button x:Name="btnAchievements" Content="Utmärkelser" Command="{Binding NavigationCommand}" CommandParameter="Achievements" HorizontalAlignment="Left" VerticalAlignment="Top" Width="91" Background="#c20071" Height="44"/>
<Button x:Name="btnNotifications" Content="Notifikations
inställningar
" Command="{Binding NavigationCommand}" CommandParameter="Notification" HorizontalAlignment="Left" Background="#95b800" VerticalAlignment="Top" Width="83" Height="44"/>
</StackPanel>
命令的绑定指的是mainwindows viewmodel&#34; MainWindowViewModel&#34;
我需要将dataconmodel中的datacontext连接到我的视图UserControl。我尝试了各种技术,例如编写代码隐藏
DataContext = new MainWindowViewModel();
这不起作用,我的猜测是因为它是一个新实例并且没有显示更改。
usercontrol上的按钮应显示第二个视图。 MainWindow xaml看起来像这样
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<ContentControl Content="{Binding CurrentNavBar}" />
<Button x:Name="button" Command="{Binding AuthorizationCommand}" Content="hej" CommandParameter="Operator" HorizontalAlignment="Left" Margin="26,27,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
<Grid Grid.Row="1">
<ContentControl Content="{Binding CurrentViewModel}" />
</Grid>
答案 0 :(得分:0)
我在Abhinav Sharma建议的亲属资源约束中找到了答案。 我以这种方式将relativesource绑定添加到stackpanel的datacontext:
<StackPanel x:Name="NavBar" Orientation="Horizontal" Height="75" VertcalAlignment="Top" HorizontalAlignment="Stretch" Background="White"
Grid.Row="0" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext}" >