这是youiandreactiveui
的代码有一种更优雅的方法可以实现这一目标吗?还是有任何方法可以用iviewfor绑定代码?
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<local:TweetMediaAttachmentView ViewModel="{Binding .}"/>
<Button
x:Name="removeButton"
Command="{Binding DataContext.RemoveAttachmentCommand, ElementName=attachmentsItemsControl}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
这是一个已知问题。一种解决方案是在此处提出的那样使用私人资源:访问datatemplate的访问父dataContext
几对不同的选项,所有选项都将使用IViewFor
功能。
首先是使用ReactiveUi的OneWayBind功能绑定到ItemsControl.ItemsSource
时,如果您不设置ItemTemplate
,它将搜索关联的IViewfor<View model>
。
另一个您可以使用ItemTemplate并使用ViewModelViewHost控件并设置视图模型。这几乎与第一种方法相同。
对于命令绑定,我会将命令传递到视图模型中,因此您不必担心数据上下文绑定。