0

I have with succes managed to implement this solution found here: WPF circle progress bar

My only problem is that my AXML uses:

xmlns:local="clr-namespace:MyNameSpace"

<local:CircularProgressBar HorizontalAlignment="Center" VerticalAlignment="Center"
     SegmentColor="#FF878889" StrokeThickness="8" Percentage="100" />
                <local:CircularProgressBar HorizontalAlignment="Center" VerticalAlignment="Center"
     Percentage="{Binding Value, ElementName=slider}" SegmentColor="#026873" StrokeThickness="8" />

How do I hide the CircleProgressBar on load and how do I update or start it from my WPF application's code?

Currently the progress indicator is drawn on every load.

Edit: The converter seems to be working but now the circle is drawn and nothing happens? This is my code:

BooleanToVisibilityConverter convert = new BooleanToVisibilityConverter();
            System.Globalization.CultureInfo culture = null;

            // public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            myName.Visibility =  (System.Windows.Visibility) convert.Convert(true, this.GetType(), myName, culture);

            //listBoxTrackers.Items.Clear();



            readInputFile(dialogFileName);
Community
  • 1
  • 1
ryokan
  • 125
  • 2
  • 12
  • Does your control not have a Visibility property? You should bind the Visibility property to a property on your view model (use a converter so that your VM property is just a boleean to control showing or not showing) – TYY Nov 23 '15 at 15:47
  • It only has a field called CircularProgressBar.VisibilityProperty but that is only a getter? How do I access the instance found in the AXML? :D – ryokan Nov 23 '15 at 16:01

1 Answers1

0

You can make something like this using binding. Also do not forget

xmlns:DesignInControl="clr-namespace:***"
xmlns:converter="clr-namespace:***"

MainWindow.Xaml:

<Window.Resources>
    <converter:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<Grid Visibility="{Binding Visibility, Converter={StaticResource BooleanToVisibilityConverter}}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
            <DesignInControl:CircularProgressBar HorizontalAlignment="Center" VerticalAlignment="Center"
          SegmentColor="#FF878889" StrokeThickness="25" Percentage="100" />
            <DesignInControl:CircularProgressBar HorizontalAlignment="Center" VerticalAlignment="Center"
          Percentage="{Binding Percentage}" SegmentColor="#026873" StrokeThickness="25" />
        </Grid>
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </StackPanel>
    <Slider x:Name="slider" Grid.Row="1" Maximum="100" Value="60" Visibility="Hidden"/>
</Grid>

MainWindow.cs:

namespace WpfApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new Data();
    }
}

public class Data:INotifyPropertyChanged
{
    private bool visibility;
    public bool Visibility 
    { 
        get
        {
            return this.visibility;
        }

        set
        {
            this.visibility = value;
            this.RaisePropertyChanged("Visibility");
        }
    }

    private double percentage;
    public double Percentage 
    { 
        get
        {
            return this.percentage;
        }

        set
        {
            this.percentage = value;
            this.RaisePropertyChanged("Percentage");
        }
    }

    public Data()
    {
        Action SomeWork = new Action(DoWork);
        IAsyncResult result = SomeWork.BeginInvoke(null, null);
    }

    public void DoWork()
    {
        System.Threading.Thread.Sleep(3000);
        Visibility = true;
        for (int i=0;i<100;i++)
        {
            System.Threading.Thread.Sleep(300);
            Percentage += 1;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string info)
    {
        if (PropertyChanged!=null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
}

BooleanToVisibilityConverter.cs

class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is Boolean && (bool)value)
        {
            return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is Visibility && (Visibility)value == Visibility.Visible)
        {
            return true;
        }
        return false;
    }
}
A191919
  • 3,422
  • 7
  • 49
  • 93
  • It is currently a ListBox in my app with the CircleProgressBar on top. Will this still work? :D – ryokan Nov 23 '15 at 16:13
  • Elegant solution but it does not work with a UserControl ;-) – ryokan Nov 23 '15 at 16:47
  • 1
    You does not mentioned that you have listbox. And if you have CircleProgressBar inside the listbox. You need only to change binding with FindAncestor. – A191919 Nov 24 '15 at 07:26