c# - Prism DelegateCommand 的 CanExecute 在 RaiseCanExecuteChanged 后获取空参数
问题描述
我正在尝试编写一个ListView
,其中每个项目都有一个绑定到 a 的按钮DelegateCommand
。我希望这个命令CanExecute
基于项目的布尔属性。ItemSource
是一个ObservableCollection
,我正在使用一种async
方法填充它。问题是当RaiseCanExecuteChanged()
被触发时,作为参数CanExecute
接收null
。之后,当我使用另一个 UI 绑定命令时RaiseCanExecuteChanged()
,该方法行为正常。
这是视图的一部分:
<ListView x:Name="root" ItemsSource="{Binding olVersions}" IsSynchronizedWithCurrentItem="true" HorizontalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=sFullName}" Grid.Column="0" VerticalAlignment="Center"></TextBlock>
<Button Grid.Column="1" Content="Run" CommandParameter="{Binding}" Command="{Binding ElementName=root, Path=DataContext.cRunVersionCommand}"></Button>
<Button Grid.Column="2" Content="Download" CommandParameter="{Binding}" Command="{Binding ElementName=root, Path=DataContext.cDownloadVersionCommand}"></Button>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是视图模型:
public class VersionsTabViewModel : BindableBase
{
public ObservableCollection<CVersion> olVersions { get; private set; }
public DelegateCommand<CVersion> cRunVersionCommand { get; private set; }
public DelegateCommand<CVersion> cDownloadVersionCommand { get; private set; }
private IVersionManager c_version_manager;
public VersionsTabViewModel(IVersionManager cVersionManager)
{
olVersions = new ObservableCollection<CVersion>();
cRunVersionCommand = new DelegateCommand<CVersion>(v_try_run_version, b_can_run);
cDownloadVersionCommand = new DelegateCommand<CVersion>(v_try_download_version);
c_version_manager = cVersionManager;
v_read_available_versions();
}
private async void v_read_available_versions()
{
List<CVersion> l_versions = await c_version_manager.lGetVersions();
List<CVersion> l_versions_sorted = l_versions.OrderByDescending(cVersion => cVersion.cSemanticVersion).ToList();
olVersions.Clear();
olVersions.AddRange(l_versions_sorted);
cRunVersionCommand.RaiseCanExecuteChanged();
}
private void v_try_run_version(CVersion cVersionToRun)
{
MessageBox.Show($"Run {cVersionToRun.sFullName}", "Run"); //TODO
}
private void v_try_download_version(CVersion cVersionToDownload)
{
MessageBox.Show($"Download {cVersionToDownload.sFullName}", "Download"); //TODO
cRunVersionCommand.RaiseCanExecuteChanged();
}
private bool b_can_run(CVersion cVersion)
{
return cVersion?.bIsInstalled ?? false;
}
}
解决方案
似乎这是命令绑定的一般问题,这里有一个相关问题。为了使其适用于您的情况,您必须将您的ElementName
绑定更改root
为Command
从
Command="{Binding ElementName=root, Path=DataContext.cRunVersionCommand}"
像这样绑定到父ListView
控件的相对源。
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListView}}, Path=DataContext.cRunVersionCommand}"
推荐阅读
- selenium - UI 自动化 - 我的 UI 上的元素有 ember ids ,随着新 UI 元素的添加而频繁更改。如何使用 id 进行自动化?
- javascript - 如何使用 ReactJS 使多个选择框动态化
- javascript - 如何在不有效破坏单词的情况下将句子分成固定长度的块?
- php - 如何在laravel中打印分页内容的所有页面?
- google-cloud-platform - bq 数据集中有更多表会减慢进程吗?
- python - 但是当我运行这个程序时,我得到一个这样的错误:“PermissionError: [Errno 13] Permission denied: 'test1.txt'”
- python - 使用python从收集的站点数据中查找特定字符串并打印
- go - 我们如何将 gcc-go 安装到 AIX 机器 power-pc-7 bigendian 64 位?
- xampp - xampp 无法在 localhost(127.0.0.1) 上运行,但可以在 (192.168.XX) 上运行
- javascript - 如何从多步列表中选择值