为什么当我设置打开对话框命令时我的按钮被禁用?

Why is my button disabled when I set the open dialog command?

我正在使用 Material Desgin for WPFDialogHost,当我设置打开对话框的命令时,我的按钮被禁用,即使使用 IsEnabled="True" 属性。

这是我的按钮 XAML 代码:

<Button IsEnabled="True" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}" TabIndex="16" x:Name="ButtonSalvar" BorderBrush="#FF2A5500" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Grid.Column="2" Grid.ColumnSpan="2"  Height="50" Margin="10 0 20 20" Background="#FF2A5500">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Stretch">
        <materialDesign:PackIcon Kind="Check" Width="40" Height="40" Margin="0 0 10 0" />
        <TextBlock Text="Salvar" VerticalAlignment="Center" FontSize="30" HorizontalAlignment="Center" />
    </StackPanel>
</Button>

如果您没有指定 CommandTarget,您需要将按钮放在 DialogHost 内。

<materialDesign:DialogHost>
   <materialDesign:DialogHost.DialogContent>
       <!-- Your dialog content -->
   </materialDesign:DialogHost.DialogContent>
   <Button Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"
           Content="Show"/>
</materialDesign:DialogHost>

或者,通过 x:Name="YourDialogHost" 命名对话框主机并将其设置为按钮的 CommandTarget,如果它不在您的对话框主机内。

<Button Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"
        CommandTarget="{Binding ElementName=YourDialogHost}"
        Content="Show"/>