在使用MVVM和Template10的UWP-Windows 10 C#/ XAML应用程序中,我目前正试图通过以下方式将ListView项目中的按钮绑定到ViewModel中的方法:
<Page.Resources>
<DataTemplate x:DataType="vm:ViewModel" x:Key="TermListViewTemplate">
<Grid>
<ListView x:Name="termListView"
ItemsSource="{x:Bind childModels, Mode=OneWay}"
ItemClick="EntityListClick">
<ListView.ItemTemplate>
<DataTemplate x:DataType="gl:Term">
<Grid HorizontalAlignment="Stretch">
<StackPanel>
<Button Click="EditButton_Click">
<SymbolIcon Symbol="Edit"></SymbolIcon>
</Button>
<Button Click="DeleteButton_Click">
<SymbolIcon Symbol="Delete"></SymbolIcon>
</Button>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</Page.Resources>
EntityListClick
,EditButton_Click
和DeleteButton_Click
是代码隐藏文件中的所有方法,然后在ViewModel
中调用方法,如下所示:
private void EntityListClick(object sender, ItemClickEventArgs e)
{
ViewModel.termClick((Term)e.ClickedItem);
}
private void EditButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ViewModel.editTerm((Term)(sender as Button).DataContext);
}
private void DeleteButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ViewModel.deleteTerm((Term)(sender as Button).DataContext);
}
如何在此方案中正确实现MVVM模式,以便在ViewModel.termClick
中直接绑定到ViewModel.editTerm
,ViewModel.deleteTerm
和ViewModel
,同时将正确的参数传递给ViewModel
?
此外,我在XAML中有一个按钮,其Click
事件处理程序附加到后面的代码中的以下方法:
private async void AddTerm_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
await ViewModel.addTermAsync();
ListView termListView = (ListView)((Grid)((Button)sender).Parent).Children[2];
termListView.ScrollIntoView(termListView.Items[termListView.Items.Count - 1]);
}
再次,是否有一些方法可以直接将点击处理程序直接链接到ViewModel,以便可以滚动ListView? (也许某种方式将ListView的整个实例绑定到类型&#34; ListView&#34;在ViewModel中的属性?)