【发布时间】:2016-05-26 09:48:53
【问题描述】:
我有一个 ItemsControl 绑定到 Tasks 列表。我想将该模型Task 赋予它的视图TaskView 和视图模型TaskViewModel。
我曾尝试像这样在视图属性上绑定值:
<views:TaskView Model="{Binding}" />
然后绑定视图和视图模型之间的值:
public partial class TaskView
{
public TaskView()
{
InitializeComponent();
// Attempt to bind view.IdProperty with viewmodel.Id
var binding = new Binding("Id");
SetBinding(IdProperty, binding);
}
public Task Model
{
get { return GetValue(ModelProperty) as Task; }
set { SetValue(ModelProperty, value); }
}
public static readonly DependencyProperty ModelProperty =
DependencyProperty.Register("Model", typeof(Task), typeof(TaskView));
}
但是它不起作用。由于我是 C#/.NET 的新手,我不知道我的方向是否正确,因为 IMO 的常见问题似乎很复杂。
请注意,它会正确呈现任务列表,因此 ItemsControl 可以按预期工作。
问题:
- 如何从数据源构建视图并将模型传递给该视图? (上面解释的问题)
- 如何从该模型读取值到视图? (参见 TaskView.xaml)
- 有没有更惯用的方法?
以下是关键部分:
型号
namespace Models
{
public class Task
{
public string Id { get; set; }
public Status Status { get; set; }
// And other properties ...
}
}
列表(父级)
视图模型
public class ListViewModel : ActionViewModelBase
{
public ObservableCollection<Task> Tasks { get; set; }
}
查看
<ItemsControl x:Name="Tasks"
Grid.Row="1"
Grid.ColumnSpan="2"
ItemsSource="{Binding Path=Tasks, Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="models:Task">
<!-- TODO : Bind model to task view/viewmodel -->
<views:TaskView Model="{}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
任务(子)
TaskView.xaml.cs
public partial class TaskView
{
public TaskView()
{
InitializeComponent();
}
public Task Model
{
get { return GetValue(ModelProperty) as Task; }
set { SetValue(ModelProperty, value); }
}
public static readonly DependencyProperty ModelProperty =
DependencyProperty.Register("Model", typeof(Task), typeof(TaskView));
}
TaskView.xaml
<UserControl>
<UserControl.DataContext>
<viewModels.TaskViewModel />
</UserControl.DataContext>
<Border>
<!-- TODO : Read model attributes -->
<Grid Background="{Binding Path=Model.Status, Converter={StaticResource StatusBackgroundColourConverter}}">
<!-- ... -->
</Grid>
</Border>
</UserControl>
任务视图模型
class TaskViewModel : ActionViewModelBase
{
public Task Model { get; set; }
// ...
}
【问题讨论】:
-
也许我只是不认识您正在使用的模式,但是说到 MVVM,您不应该从模型创建视图模型并从视图模型创建视图,而不是让视图处理模型?
-
@grek40 这对我来说看起来不正确。根据 MVVM 模式,Model 不应该知道 ViewModel,ViewModel 不应该知道 View。查看取自 MSDN 文档 i-msdn.sec.s-msft.com/dynimg/IC564167.png 的图像
-
看图就明白了,view和model之间没有直接的联系。确实,模型不知道视图模型,但是:视图模型知道模型,所以说
new viewmodel(modelData)是合法的。 viewmodel 和 view 类似。 -
@grek40 我同意,但这似乎与您的第一条评论相矛盾,或者我误解了您。
标签: c# wpf xaml .net-4.5 mvvm-light