【发布时间】:2021-12-25 22:44:56
【问题描述】:
我有一个组合框和一个按钮。我希望每当我从中选择一项时,该按钮都不会被禁用
XAML:
<Border Style="{StaticResource borderMain}"
Grid.Row="7"
Grid.Column="0">
<ComboBox ItemsSource="{Binding Source={StaticResource portNames}}"
SelectedItem="{Binding SelectedPort, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
x:Name="Port_Selector" Grid.Column="0"
Text="Port Selector" Background="White"/>
</Border>
<Border Style="{StaticResource borderMain}"
Grid.Column="1"
Grid.Row="7">
<Button Content="Connect"
Command="{Binding OpenPortCommand}"
CommandParameter="{Binding SelectedPort}"
Style="{StaticResource buttonMain}"
Margin="5"/>
</Border>
命令:
public class OpenPortCommand : ICommand
{
public OpenPortVM OpenPortVM{ get; set; }
public OpenPortCommand(OpenPortVM OpenPortVM)
{
this.OpenPortVM = OpenPortVM;
}
public event EventHandler? CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object? parameter)
{
string? portCom = parameter as string;
if (!string.IsNullOrEmpty(portCom))
return true;
return false;
}
public void Execute(object? parameter)
{
OpenPortVM.ConnectPort();
}
}
我已经对其进行了调试,并检查了我用于绑定SelectedPort 的变量的值,它上面有一个值,但不知何故,我的按钮的CommandParameter 未检测到,因此CanExecute 方法运行不正常。我错过了什么吗?
更新
更新 2
【问题讨论】: