【发布时间】:2019-11-01 18:37:14
【问题描述】:
我有这个 xaml 和这个代码。 我的问题是确认没有改变。 CanExecute 只执行一次。
<Label Grid.Row="0" Grid.Column="0" Content="Connection String"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=ConnectionString,UpdateSourceTrigger=PropertyChanged}" ></TextBox>
<Button Grid.Row="1" Grid.Column="0" Content="Load" Command="{Binding BtnLoad,UpdateSourceTrigger=PropertyChanged}"></Button>
<Button Grid.Row="1" Grid.Column="1" Content="Confirm" Command="{Binding BtnConfirm}"></Button>
<Button Grid.Row="1" Grid.Column="2" Content="Add" Command="{Binding BtnAdd,UpdateSourceTrigger=PropertyChanged}"></Button>
</Grid>
public MainViewModel(ILogRepository logRepository)
{
_logRepository = logRepository;
_listOfLogs = new List<Log>();
BtnAdd = new BtnAdd(AddLog);
BtnConfirm = new BtnConfirm(ConfimLog, LogIsSelected);
BtnLoad = new BtnLoad(LoadLogTable);
}
private bool LogIsSelected()
{
// return true;
//Funktionerit im WPF nicht
if (_selectedLogItem != null)
return true;
return false;
}
【问题讨论】:
-
欢迎来到 Stack Overflow。您似乎遇到了一些您没有向我们展示的代码的问题。请通过编辑您的问题并将代码添加到您的问题中向我们展示该代码。当命令的执行能力发生变化时,您可能需要引发命令的
CanExecuteChanged事件。如果你不这样做,按钮就不会知道它需要再次调用 CanExecute。 -
您可以从这些绑定中删除
UpdateSourceTrigger=PropertyChanged。 Command 属性永远不会创建新的 Command 对象并将其分配回您的 viewmodel 属性,因此无需确切告诉它何时执行此操作。即使它愿意尝试,您的属性也可能是只读的。 -
澄清一下:您没有向我们展示的代码是命令本身。是什么决定了它能否执行?
-
Logisselected 是私有的。我认为它需要公开才能使视图获得任何价值。您还需要一些用户 ui 交互或docs.microsoft.com/en-us/dotnet/api/…,然后才能对其进行检查。