[英]How can i bind a button in listviewitem to a Command in ViewModel in Winrt
I have a NavigateToAccountsCommand RelayCommand property in the ViewModel. 我在ViewModel中有一个NavigateToAccountsCommand RelayCommand属性。 When I bind the same to a button on the page anywhere outside the ListView the command binding is working.
当我将相同的页面上的按钮绑定到ListView外的任何位置时,命令绑定正在运行。 However as soon as I move this to a ListView's DataTemplate its not working.
但是,只要我将其移动到ListView的DataTemplate,它就无法正常工作。
I have tried changing the binding from NavigateToAccountsCommand to DataContext.NavigateToAccountsCommand still not working. 我已经尝试将绑定从NavigateToAccountsCommand更改为DataContext.NavigateToAccountsCommand仍然无法正常工作。
Appreciate your help... 感谢您的帮助...
<Page
x:Class="FinancePRO.App.Views.AccountsView"
DataContext="{Binding AccountsViewModel, Source={StaticResource MainViewModelLocator}}"
mc:Ignorable="d">
<Grid>
<!--**This one is working**-->
<Button Command="{Binding NavigateToAccountsCommand}" >
<!--**This one is not working**-->
<ListView ItemsSource="{Binding AllAccounts}" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding AccountName}"/>
<Button Command="{Binding NavigateToAccountsCommand}">
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When you are inside the DataTemplate
of the ListView
, your data context is the current item of the ListView's ItemsSource
. 当您在
ListView
的DataTemplate
中时,您的数据上下文是ListView的ItemsSource
的当前项。 As there is nothing called " NavigateToAccountsCommand
" property within your AllAcounts
' each individual element, binding isn't working. 由于在
AllAcounts
的每个单独元素中没有任何名为“ NavigateToAccountsCommand
”的属性,因此绑定不起作用。
To fix that, you will need to reference something from the outside of DataTemplate
; 要解决这个问题,您需要从
DataTemplate
外部引用一些内容; following should work. 以下应该工作。 It changes the binding to reference the root Grid's
DataContext
which should have the property NavigateToAccountsCommand
accessible. 它更改绑定以引用根网格的
DataContext
,该DataContext
应该可以访问属性NavigateToAccountsCommand
。 To reference the grid, you have to add Name attribute, and then use ElementName
binding. 要引用网格,必须添加Name属性,然后使用
ElementName
绑定。
<Grid Name="Root">
<!--**This one is working**-->
<Button Command="{Binding NavigateToAccountsCommand}" >
<!--**This one is not working**-->
<ListView ItemsSource="{Binding AllAccounts}" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding AccountName}"/>
<Button Command"{Binding ElementName=Root, Path=DataContext.NavigateToAccountsCommand}">
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
You can use 您可以使用
<Button x:Name="cmdTabItemCloseButton"
Style="{StaticResource TabItemCloseButtonStyle}"
Grid.Column="1" Margin="15,0,0,0"
Command="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type ListView}},
Path=DataContext.NavigateToAccountsCommand}"
CommandParameter="{Binding}"/>
I had a similar problem (Win RT) which I solved by just using: 我有一个类似的问题(Win RT),我通过使用解决了:
<GridView
x:Name="itemGridView"
ItemClick="ItemView_ItemClick"
IsItemClickEnabled="True"/>
and then in the Page class: 然后在Page类中:
private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
//e is the object being clicked
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.