我当然希望这个问题很容易回答,但这已经过去了几天,我还没有想出如何在UserControl之间共享数据。在多个视图中共享ViewModel
下面的页面有两个选项卡,并且它们的源被引用。它还有一个标签为Start的按钮,用于访问ViewModel中的命令。
我遇到的问题是数据实际上在选项卡内。我将它们设置为使用相同的ViewModel,并且它们都创建了自己的ViewModel实例,因此数据永远不会显示到“开始”按钮。我希望从那以后。
这是一个相当简单的程序,我真的没有看到需要有3个独立的ViewModels只包含单个元素,但也许这就是我需要做的。总而言之,我仍然需要收集所有数据,并在按下该开始按钮时将其提交给外部实体。
我很抱歉,如果这是一个问题之前已经问过,但我相对较新的C#所以我不完全确定我在问什么。提前致谢!
<UserControl
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:mui="http://firstfloorsoftware.com/ModernUI"
x:Class="DeployWiz.Pages.Config"
mc:Ignorable="d"
xmlns:local="clr-namespace:DeployWiz.ViewModel"
d:DesignHeight="356.978" d:DesignWidth="333.582">
<Grid Style="{StaticResource ContentRoot}" Margin="16,28,16,5" >
<Grid.DataContext>
<local:ComputerViewModel/>
</Grid.DataContext>
<mui:ModernTab x:Name="tabList" Layout="List" SelectedSource="/Views/ComputerView.xaml" Margin="0,0,0,40">
<mui:ModernTab.Links>
<mui:Link DisplayName="Settings" Source="/Views/ComputerView.xaml" />
<mui:Link DisplayName="Applications" Source="/Views/ApplicationView.xaml" />
</mui:ModernTab.Links>
</mui:ModernTab>
<Button Content="Start" HorizontalAlignment="Center" Margin="0,0,0,5" VerticalAlignment="Bottom" Width="75" Command="{Binding Path=StartTask}"/>
</Grid>
</UserControl>