【发布时间】:2016-07-29 07:27:30
【问题描述】:
我正在尝试使用 WPF 和 MVVM 协议构建应用程序。该应用程序有一个窗口和多个视图(用户控件)。其中一个视图内部还有子视图以显示不同的数据。
我的问题 我不明白如何将数据绑定到子视图。我试图了解数据是如何绑定的,但只能设法将数据向下绑定一层。
代码
这里是一些代码。我希望它能让我更容易理解我的问题。
App.xaml.cs
public partial class App : Application
{
private Engine _engine;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow window = new MainWindow();
_engine = new Engine("test");
var viewModel = new MainWindowViewModel(_engine);
EventHandler handler = null;
handler = delegate
{
viewModel.RequestClose -= handler;
window.Close();
};
viewModel.RequestClose += handler;
window.DataContext = viewModel;
window.Show();
}
}
这里是我创建引擎对象并将其传递给我想要进一步绑定视图层次结构的 MainWindowViewModel 的地方。
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:"
xmlns:ViewModels="clr-namespace:ViewModels"
xmlns:View="clr-namespace:Views"
x:Class="MainWindow"
Title="title" Height="800" Width="1200"
WindowStartupLocation="CenterScreen" Icon="Resources/Images/logo.png"
>
<DockPanel Margin="0" Background="#FF4F4F4F" LastChildFill="True">
<Menu DockPanel.Dock="Top" Height="20">
<MenuItem Header="File">
<MenuItem Header="Exit"/>
</MenuItem>
</Menu>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" MinWidth="200" MaxWidth="200"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<View:TabView Grid.Column="0"/>
<View:WorkspaceView Grid.Column="1"/>
</Grid>
</DockPanel>
WorkspaceView.xaml
<UserControl x:Class="Views.WorkspaceView"
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:Views"
xmlns:ViewModels="clr-namespace:ViewModels"
mc:Ignorable="d"
d:DesignHeight="750"
d:DesignWidth="1000"
d:DataContext="{d:DesignInstance ViewModels:WorkspaceViewModel}"
>
<Grid>
<Label x:Name="label" Height="750" VerticalAlignment="Top" FontSize="60" Foreground="White" Content="{Binding Engine.BarCode}"/>
<Grid Margin="40">
<Grid.Background>
<ImageBrush ImageSource="/;component/Resources/Images/logo.png" Stretch="Uniform"/>
</Grid.Background>
<ContentPresenter Content="{Binding CurrentView}"/>
</Grid>
<DockPanel>
<!--ContentPresenter Content="{Binding CurrentView}"/-->
</DockPanel>
</Grid>
在这里,我正在尝试绑定{Binding Engine.BarCode},它可以工作并为我提供一个包含正确数据的字符串。但是<ContentPresenter Content="{Binding CurrentView}"/> 不会显示我在 ViewModel 中为 workspaceView 设置的当前视图。
WorkspaceViewModel.cs
public WorkspaceViewModel()
{
_currentView = new InjectorView();
}
public UserControl CurrentView
{
get { return _currentView; }
}
-
更新:
_currentView = new InjectorView();仅用于测试,currentView 应根据在另一个视图中按下的按钮而改变。*
WorkspaceView.xaml.cs
public WorkspaceView()
{
InitializeComponent();
this.DataContext = new WorkspaceViewModel();
}
InjectorView.xaml
<UserControl x:Class="Views.InjectorView"
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:Views"
xmlns:ViewModels="clr-namespace:ViewModels"
mc:Ignorable="d"
d:DesignHeight="750"
d:DesignWidth="1000"
d:DataContext="{d:DesignInstance ViewModels:InjectorViewModel}"
>
<Grid Background="#FFAEAEAE">
<Label x:Name="label1" Content="{Binding Engine.BarCode}"/>
</Grid>
</UserControl>
但是,如果我从 WorkspaceView.xaml 中删除 d:DataContext="{d:DesignInstance ViewModels:WorkspaceViewModel}" 并将 this.DataContext = new WorkspaceViewModel(); 添加到 xaml 代码的 c# 文件中,它将显示当前视图 (InjectorView)。现在唯一的问题是当我尝试在 InjectorView {Binding Engine.BarCode} 中绑定一些数据时,它不会显示与以前相同的字符串(我猜它不再是同一个对象实例了??)
我错过了什么?我对 MVVM 和 wpf 的解释是否完全错误?
(由于产品的原因,我不得不删除一些代码(例如命名空间))
【问题讨论】:
标签: c# .net wpf mvvm data-binding