avechuche
avechuche

Reputation: 1560

Use ProgressRing

HI!. I have a problem

<Button Margin="5"
        Visibility="{Binding IsVisibleProgressRing, Converter={StaticResource InvertBoolToVisibilityConverter}, Mode=TwoWay}">
        Content="Search"
</Button>

<mahApps:ProgressRing Margin="5"
                      Height="32" Width="32"
                      DockPanel.Dock="Right"
                      IsActive="{Binding IsActiveProgressRing, Mode=TwoWay}" 
                      Visibility="{Binding IsVisibleProgressRing, Mode=TwoWay}"/>

i have too

public class InvertBoolToVisibilityConverter : IValueConverter
{
    private readonly BooleanToVisibilityConverter _converter = new BooleanToVisibilityConverter();

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var result = _converter.Convert(value, targetType, parameter, culture) as Visibility?;
        return result == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var result = _converter.ConvertBack(value, targetType, parameter, culture) as bool?;
        return result != true;
    }
}

In VM have

private bool _isActiveProgressRing;

private Visibility _isVisibleProgressRing = Visibility.Collapsed;

IsActiveProgressRing = true;

IsVisibleProgressRing = Visibility.Visible;

.....................
.....................
<!-- SEARCH IN DB -->
.....................
.....................

IsVisibleProgressRing = Visibility.Collapsed;

IsActiveProgressRing = false;

I need it is that when I look for in a database, the search button is hidden and the "ProgressRing" visible and active. When the search is over, everything returns to normal. The problem is that the "progressRing" appears at the end of the search in the database and the button, never disappears.

Upvotes: 0

Views: 459

Answers (1)

Special Sauce
Special Sauce

Reputation: 5594

If you are running the database search off the UI thread (in other words, the method is being called due to a button click or other GUI control event), it will make the GUI unresponsive and not update properly while the UI thread is occupied with the database search.

The database search should be moved to a background thread like Dennis suggested and, if need be, the background thread can update the GUI using this sort of construct to instruct the GUI thread to handle the desired GUI change (the background thread should never try to update the GUI directly):

this.Invoke((MethodInvoker)delegate {
    // this next line will run on the UI thread regardless of which thread called .Invoke()
    someLabel.Text = numDbResults.ToString() + " database matches found";
});

Upvotes: 1

Related Questions