毁灭
活跃的成员
- 已加入
- 2020年3月28日
- 留言内容
- 36
- 编程经验
- 1-3
我有一个mainViewModel和一个主窗口,其中有4个用户控件绑定到各自的视图模型。
MainViewModel基于inputViewModel.cs和InputView用户控件进行一些计算,并在resultView中显示结果。
我想在MainViewModel中使用计算结果的数据上下文。到目前为止,我已经使用mainWindow.xaml.cs类设置新窗口的相同dataContext,方法如下:
这是MainWindow的xaml代码:
这是ResultView UserControl xaml代码:
当我单击“计算”按钮时,“我的新窗口”打开,并显示与MainWindow相同的ResultView用户控件,但我想创建自定义用户控件,该控件将放置在另一个名为ResultWindow的窗口中,并且可以使用下拉组合键切换用户控件。
所有用户控件都必须继承与mainWindow相同的数据上下文。有人可以通过创建datacontext的各个引用为我指出正确的方向,以指示要在用户控件之间共享dataContext的步骤吗?
MainViewModel基于inputViewModel.cs和InputView用户控件进行一些计算,并在resultView中显示结果。
我想在MainViewModel中使用计算结果的数据上下文。到目前为止,我已经使用mainWindow.xaml.cs类设置新窗口的相同dataContext,方法如下:
C#:
namespace SweWPF
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModels.MainViewModel();
}
public ViewModels.MainViewModel ViewModel { get { return (ViewModels.MainViewModel)DataContext; } }
private void Button_Click(object sender, RoutedEventArgs e)
{
ResultWindow resultWindow = new ResultWindow();
resultWindow.DataContext = this.DataContext;
resultWindow.Show();
}
}
这是MainWindow的xaml代码:
C#:
<Window x:Class="SweWPF.MainWindow"
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"
mc:Ignorable="d"
xmlns:vm="clr-namespace:SweWPF.ViewModels"
xmlns:vw="clr-namespace:SweWPF.Views"
d:DataContext="{d:DesignInstance Type=vm:MainViewModel, IsDesignTimeCreatable=True}"
Title="Manglam Stellaris"
WindowState="Maximized"
Height="384" Width="733">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="280" />
<ColumnDefinition Width="4*" />
</Grid.ColumnDefinitions>
<vw:ConfigView Grid.Row="0" Grid.ColumnSpan="2" DataContext="{Binding Config}" />
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Auto" Grid.RowSpan="2"/>
<vw:InputView DataContext="{Binding Input}" Grid.RowSpan="2" />
<Button Grid.Row="1" Content="Calculation" Padding="16,8" Margin="0,244,21,0" Command="{Binding DoCalculationCommand, Mode=OneWay}" Click="Button_Click" />
</Grid>
<vw:ResultView Grid.Row="1" Grid.Column="1" DataContext="{Binding Result}"/>
</Grid>
</Window>
这是ResultView UserControl xaml代码:
C#:
<UserControl x:Class="SweWPF.Views.ResultView"
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:vm="clr-namespace:SweWPF.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vm:CalculationResultViewModel, IsDesignTimeCreatable=True}"
d:DesignHeight="360" d:DesignWidth="629">
<Grid>
<ScrollViewer Padding="4">
<StackPanel>
<!-- Date 和 calculations -->
<Label Style="{StaticResource SectionLabel}">Date 和 calculation</Label>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Date UTC" Style="{StaticResource CaptionLabel}"/>
<Label Grid.Row="1" Grid.Column="0" Content="{Binding DateUTC}" />
<Label Grid.Row="0" Grid.Column="1" Content="Julian Day" Style="{StaticResource CaptionLabel}"/>
<Label Grid.Row="1" Grid.Column="1" Content="{Binding JulianDay}" />
<Label Grid.Row="0" Grid.Column="2" Content="Delta T" Style="{StaticResource CaptionLabel}"/>
<Label Grid.Row="1" Grid.Column="2" Content="{Binding DeltaTSec}" />
<Label Grid.Row="2" Grid.Column="0" Content="Ephemeris Time" Style="{StaticResource CaptionLabel}"/>
<Label Grid.Row="3" Grid.Column="0" Content="{Binding EphemerisTime}" />
<Label Grid.Row="2" Grid.Column="1" Content="Sideral Time" Style="{StaticResource CaptionLabel}"/>
<Label Grid.Row="3" Grid.Column="1" Content="{Binding SideralTime, ConverterParameter=hour, Converter={StaticResource DoubleToTimeFormatConverter}}" />
<Label Grid.Row="4" Grid.Column="0" Content="Mean Ecliptic Obliquity" Style="{StaticResource CaptionLabel}"/>
<Label Grid.Row="5" Grid.Column="0" Content="{Binding MeanEclipticObliquity, Converter={StaticResource DoubleToDegreesFormatConverter}}" />
<Label Grid.Row="6" Grid.Column="0" Content="True Ecliptic Obliquity" Style="{StaticResource CaptionLabel}"/>
<Label Grid.Row="7" Grid.Column="0" Content="{Binding TrueEclipticObliquity, Converter={StaticResource DoubleToDegreesFormatConverter}}" />
<Label Grid.Row="4" Grid.Column="1" Content="Nutation in Longitude" Style="{StaticResource CaptionLabel}" />
<Label Grid.Row="5" Grid.Column="1" Content="{Binding NutationLongitude, Converter={StaticResource DoubleToDegreesFormatConverter}}" />
<Label Grid.Row="6" Grid.Column="1" Content="Nutation Obliquity" Style="{StaticResource CaptionLabel}" />
<Label Grid.Row="7" Grid.Column="1" Content="{Binding NutationObliquity, Converter={StaticResource DoubleToDegreesFormatConverter}}" />
<Label Grid.Row="4" Grid.Column="2" Content="ARMC" Style="{StaticResource CaptionLabel}" />
<Label Grid.Row="5" Grid.Column="2" Content="{Binding ARMC, Converter={StaticResource DoubleToDegreesFormatConverter}}" />
</StackPanel>
</ScrollViewer>
</Grid>
</UserControl>
当我单击“计算”按钮时,“我的新窗口”打开,并显示与MainWindow相同的ResultView用户控件,但我想创建自定义用户控件,该控件将放置在另一个名为ResultWindow的窗口中,并且可以使用下拉组合键切换用户控件。
所有用户控件都必须继承与mainWindow相同的数据上下文。有人可以通过创建datacontext的各个引用为我指出正确的方向,以指示要在用户控件之间共享dataContext的步骤吗?