Is it possible to realize this loading button with the built-in methods? #3149
-
Hi, I want to realize loading buttons like below, what should I do? thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So here is a working example. Here is the code for it: <Button Command="{Binding ClickCommand}">
<Button.Resources>
<!--
This is a Material Design converter
Under normal circumstances, it should be declared higher up (at the UserControl/Window/App level).
The xmlns should be declared at the higher level too.
-->
<converters:BooleanToVisibilityConverter
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf"
x:Key="InverseBooleanToVisibilityConverter" FalseValue="Visible" TrueValue="Collapsed" />
</Button.Resources>
<Button.Content>
<StackPanel Orientation="Horizontal">
<!--
Sets the foreground of the ProgressBar to match the text of the button
Sets the Visibily of the progress bar to only show when the button is disabled
-->
<ProgressBar IsIndeterminate="True"
Style="{StaticResource MaterialDesignCircularProgressBar}"
Foreground="{Binding Foreground, ElementName=Text}"
Visibility="{Binding IsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Converter={StaticResource InverseBooleanToVisibilityConverter}}"
Margin="0,0,4,0"/>
<TextBlock Text="Click Me" x:Name="Text"/>
</StackPanel>
</Button.Content>
</Button> The ViewModel for this then looks like this: public partial class MainWindowViewModel : ObservableObject
{
//This is using the source generators from CommunityToolkit.Mvvm to generate a RelayCommand
//See: https://learn.microsoft.com/dotnet/communitytoolkit/mvvm/generators/relaycommand
//and: https://learn.microsoft.com/windows/communitytoolkit/mvvm/relaycommand
[RelayCommand]
private async Task Click()
{
await Task.Delay(2_000);
}
} I left several comments in the code, but there is a lot of subtlety in there so let me explain the interaction. |
Beta Was this translation helpful? Give feedback.
So here is a working example.
Here is the code for it: