WPF 中的 ReactiveUI:如何将父级 ViewModel 中的命令绑定到 ItemsControl.ItemTemplate 中的按钮?



这是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控件并设置视图模型。这几乎与第一种方法相同。

对于命令绑定,我会将命令传递到视图模型中,因此您不必担心数据上下文绑定。

最新更新