【发布时间】:2016-06-20 20:55:32
【问题描述】:
我正在尝试制作一个简单的任务管理器,作为在 WPF 应用程序中学习 prism 和 MVVM 的一种方式。
目前我有一个 MainWindow 和 MainWindowViewModel、一个 TaskList 和 TaskListViewModel,以及一个 Task 和 TaskViewModel。
当我加载应用程序时,我有 1 个按钮“新任务”最终目标是单击“新任务”按钮,然后让它在“任务列表”中添加一个新的“任务”,但是我不能似乎弄清楚如何从 MainWindowViewModel 添加控件。
主窗口视图模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Prism.Mvvm;
using Prism.Commands;
using WpfApplication3.Views;
namespace WpfApplication3.ViewModels
{
public class MainWindowViewModel : BindableBase
{
public DelegateCommand NewTask { get; set; }
public MainWindowViewModel()
{
NewTask = new DelegateCommand(Execute);
}
private void Execute()
{
MessageBox.Show("Worked");
}
}
}
主窗口 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:WpfApplication3"
xmlns:prism="http://prismlibrary.com/"
xmlns:local1="clr-namespace:WpfApplication3.Views" x:Class="WpfApplication3.Views.MainWindow"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_loaded">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="177*"/>
<ColumnDefinition Width="340*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.ColumnSpan="2">
<Button Grid.Row="0" x:Name="btnNew" Content="New Task" Command="{Binding NewTask}"/>
</StackPanel>
<StackPanel x:Name="taskList" Height="Auto" Grid.Row="1" VerticalAlignment="Top" Width="Auto"/>
<local1:TaskList HorizontalAlignment="Left" VerticalAlignment="Top" Height="Auto" Grid.Row="1" Width="Auto"/>
</Grid>
</Window>
任务列表 XAML:
<UserControl x:Class="WpfApplication3.Views.TaskList"
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:WpfApplication3.Views"
mc:Ignorable="d"
d:DesignHeight="250" d:DesignWidth="177">
<Grid>
<StackPanel HorizontalAlignment="Left" Height="Auto" VerticalAlignment="Top" Width="Auto"/>
</Grid>
</UserControl>
任务 XAML:
<UserControl x:Class="WpfApplication3.Views.Task"
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:WpfApplication3.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="177">
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="50" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="177"/>
</Grid>
</UserControl>
问题:
如何动态添加“任务”用户控件到“任务列表”用户控件并保留 MVVM,而不在主窗口视图上添加代码隐藏?
我的直接想法是我需要通过 ViewModel,但我似乎无法让 MainWindowViewModel 看到视图对象(我也不确定它应该)
【问题讨论】:
-
希望大家可以看看列表框控件的ItemTemplate。您可以在哪里使用 UserControl 挂钩数据模板。
-
TaskList和Task View的作用是什么? DataEntry 的任务视图是否来自用户?
-
好吧,我是 WPF 和 Prism 的新手,所以我可能只是不明白执行此操作的正确方法。 TaskList 控件的目的是保存“任务”控件,它将任务的详细信息加载到另一个部分。因此,当您单击任务时,它会加载任务的详细信息以供用户查看。
-
我看到你是 wpf 的新手,但我喜欢你的想法。继续前进,你会摇滚;)