2

i found a program and am trying to port it from C# to WPF, im doing well so far, but i have a problem with UserControl. I cannot convert it to WPF, since my knowledge is limited. On C# we used OnPaint, on WPF OnRender, i cant get a clue how to implement same UserControl in WPF Here is the code:

public partial class DownloadBar : UserControl
{
    public DownloadBar()
    {
        InitializeComponent();
    }


    public enum DownloadBarState
    {
        Setup,
        Available,
        Playable
    }

    protected double percent = 0.0f;

    [Category("Download Bar Properties")]
    [DefaultValue(true)]
    public double Value
    {
        get
        {
            return percent;
        }

        set
        {

            if (value < 0)
                value = 0;
            else if (value > 100)
                value = 100;

            percent = value;

            this.Invalidate();
        }
    }

    protected DownloadBarState dBarState = DownloadBarState.Setup;

    [Category("Download Bar Properties")]
    public DownloadBarState BarState
    {
        get
        {
            return dBarState;
        }
        set
        {
            dBarState = value;

            this.Invalidate();
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        Graphics g = e.Graphics;

        switch (dBarState)
        {
            case DownloadBarState.Setup:
                g.DrawImage(Properties.Resources.dbar_setup, 0, 0);
                g.DrawImage(Properties.Resources.red_progressbar, 5, 35, (int)((Value / 100) * this.Width), 10);
                break;

            case DownloadBarState.Available:
                g.DrawImage(Properties.Resources.dbar_available, 0, 0);
                g.DrawImage(Properties.Resources.yellow_progressbar, 5, 35, (int)((Value / 100) * this.Width), 10);
                break;

            case DownloadBarState.Playable:
                g.DrawImage(Properties.Resources.dbar_playable, 0, 0);
                g.DrawImage(Properties.Resources.DownloadProgressBarGreen, 5, 35, (int)((Value / 100) * this.Width), 10);
                break;
        }
    }
}

And here is the second part:

public partial class DownloadProgressBar : UserControl
{
    public DownloadProgressBar()
    {
        InitializeComponent();
    }

    public enum ProgressBarColor
    {
        Green,
        Blue,
        Red,
        Yellow
    }

    protected double percent = 0.0f;

    [Category("Behavior")]
    [DefaultValue(true)]
    public double Value
    {
        get
        {
            return percent;
        }

        set
        {

            if (value < 0)
                value = 0;
            else if (value > 100)
                value = 100;

            percent = value;

            progressBarPictureBox.Size = new Size((int)((value / 100) * this.Width), this.Height);

            this.Invalidate();
        }
    }

    public void DownloadColor(ProgressBarColor color)
    {
        switch (color)
        {
            case ProgressBarColor.Green:

                progressBarPictureBox.BackgroundImage = Properties.Resources.DownloadProgressBarGreen;

                break;
        }
    }

}

Read about Invalidate, and that there is no such option in WPF also googled about WPF OnRender, but still have no idea how to do this. Any help is really appreciated!

Here is some screenshots of what im trying to achieve No Data Loaded Some Data Loaded Download Complete

edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
Ivan Zhivolupov
  • 1,107
  • 2
  • 20
  • 39
  • 3
    `C# to WPF` What does it mean? – EZI Jul 06 '14 at 16:59
  • @EZI LOL... I think the OP means winforms to WPF. – Federico Berasategui Jul 06 '14 at 16:59
  • Yep, edited topic header – Ivan Zhivolupov Jul 06 '14 at 17:02
  • FYI, C# is a `language`. What you really mean is to migrate from `winforms` (which is a deprecated UI framework from the days of .Net 2.0) to WPF. See [Transitioning from winforms to WPF](http://stackoverflow.com/a/15684569/643085). Basically you need to delete all that horrible code and use proper DataBinding instead. – Federico Berasategui Jul 06 '14 at 17:02
  • It's better if you post a screenshot of what you need so we can tell you the right way to do it in WPF. Almost none of the useless winforms code will be usable in current technologies. – Federico Berasategui Jul 06 '14 at 17:04
  • See similar question: http://stackoverflow.com/questions/3340213/c-sharp-wpf-onpaint-method-alternative – edtheprogrammerguy Jul 06 '14 at 17:11
  • 1
    @edtheprogrammerguy that answer is misleading in so many ways. Just by saying "stick to forms" the answer is giving the impression that winforms is actually usable and a valid choice for .Net UIs when it's clearly NOT. – Federico Berasategui Jul 06 '14 at 17:14
  • @IvanZhivolupov in your screenshot I only see some text over a black background, which is inconsistent with the code you posted about a Progress Bar. Can you please clarify what you're trying to achieve? does the black background change color or something – Federico Berasategui Jul 06 '14 at 17:43

1 Answers1

0

You can create a WPF UserControl that looks something like this (I'll leave actual styling up to you):

<StackPanel Orientation="Horizontal">
    <StackPanel.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="10"/>
        </Style>
    </StackPanel.Resources>
    <TextBlock Visibility="{Binding Path=SetupStage, Converter={StaticResource StageToVisibilityConverter}, ConverterParameter=1}">Setup</TextBlock>
    <TextBlock Visibility="{Binding Path=SetupStage, Converter={StaticResource StageToVisibilityConverter}, ConverterParameter=2}">Available</TextBlock>
    <TextBlock Visibility="{Binding Path=SetupStage, Converter={StaticResource StageToVisibilityConverter}, ConverterParameter=3}">Playable</TextBlock>
</StackPanel>

With the Converter being:

public class StageToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((int)value == (int)parameter)
        {
            return Visibility.Visible;
        }
        return Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

SetupStage in this case is an int that represents which state in the Setup you are at. It could also be an enum.

Troels Larsen
  • 4,462
  • 2
  • 34
  • 54