Search code examples
wpfxamlmvvmmaterial-design-in-xaml

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


I am using Material Desgin for WPF 's DialogHost and when I set the command to open the dialog my button is disabled, even with the IsEnabled="True" property.

Here's my button XAML code:

<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>

Solution

  • You need to place your button inside of your DialogHost, if you do not specify a CommandTarget.

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

    Alternatively, name the dialog host via x:Name="YourDialogHost" and set it as CommandTarget of your button, if it is not inside of your dialog host.

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