我已经阅读了有关命令在 ListView 内部的工作方式有何不同的文章,因此我尝试了该代码,但是当我单击时没有任何反应。我正在使用 Template10。我发现的大多数示例都是针对具有不兼容部分的 WPF 的。只需要最低限度的点击按钮来调用下面的方法。我的代码的相关部分是:
<ListView x:Name="lvMain"
ItemsSource="{Binding LeadSpeakerItems}"
SelectedItem="{Binding Lsi}">
<ListView.ItemTemplate>
...
<Button Content="Details"
Command="{Binding ElementName=Root, Path=RunCommand}"
Grid.Column="1" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
代码:
public ICommand RunCommand { get; private set; }
public MainPageViewModel()
{
if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
{
LeadSpeakerItems.Add(new LeadSpeakerItem {
VelocifyLeadTitle = "The is the lead title that says somrthing about something and her a number 234-456-3454",
VelocifyFirstName = "BobbiMinajobi",
VelocifyLastName = "Luciferdissikusliskus",
VelocifyLoanAmount = 254000.00,
VelocifyHomeValue = 278000.00
});
}
RunCommand = new DelegateCommand<object>(OnRunCommand, CanRunCommand);
}
private void OnRunCommand(object obj)
{
// use the SelectedCustomer object here...
}
private bool CanRunCommand(object obj)
{
return true;
}
编辑 1:
当按钮或 ListView 项目被选中时,我如何获得该特定项目?我正试图在发生这种情况时运行这段代码。我遗漏了一些东西。
set
{
Set(ref selectedItem, value);
}
最佳答案
假设 Root 是您的页面或另一个将 View 模型作为 DataContext
的控件,您应该将 XAML 更改为:
<Button Content="Details"
Command="{Binding ElementName=Root, Path=DataContext.RunCommand}"
Grid.Column="1" />
因为 RunCommand
本身对于您的 Root 对象是未知的,但是 DataContext
(您的虚拟机)是。
关于c# - 带有按钮的 ListView 中的 UWP/MVVM 数据绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34344669/