我想在双击任何列表项时显示对话框。但程序的流程永远不会进入ShowTextCommand属性。我得到名称列表(工作正常),但我无法得到对话框。这是我的XAML:
<ListView ItemsSource="{Binding List}" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}">
<TextBlock.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding ShowTextCommand, UpdateSourceTrigger=PropertyChanged}"></MouseBinding>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是我的Command类:
public class EnterTextCommand : ICommand
{
public EnterTextCommand(TekstoviViewModel vm)
{
ViewModel = vm;
}
private TekstoviViewModel ViewModel;
#region ICommand interface
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return true;
// return ViewModel.CanExecute;
}
public void Execute(object parameter)
{
ViewModel.EnterText();
}
#endregion
}
和视图模型
private ICommand command;
public ICommand ShowTextCommand
{
get
{
if (command == null)
command = new EnterTextCommand(this);
return command;
}
internal void EnterText()
{
MessageBox.Show("Event Success");
}
有人可以帮忙吗?
答案 0 :(得分:1)
您的DataTemplate
找不到command
,请使用ElementName
绑定指定其完整路径
<ListView ItemsSource="{Binding List}" x:Name="MainList">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}">
<TextBlock.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding DataContext.ShowTextCommand, UpdateSourceTrigger=PropertyChanged,ElementName=MainList}"></MouseBinding>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>