WinForms到WPF迁移

时间:2014-07-06 16:58:19

标签: c# wpf winforms xaml user-controls

我发现了一个程序,并且我正在尝试将它从C#移植到WPF,到目前为止我做得很好,但是我遇到了UserControl的问题。我无法将其转换为WPF,因为我的知识有限。在C#上我们使用OnPaint,在WPF OnRender上,我无法弄清楚如何在WPF中实现相同的UserControl 这是代码:

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

这是第二部分:

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

}

了解Invalidate,并且WPF中没有这样的选项也用Google搜索WPF OnRender,但仍然不知道如何做到这一点。任何帮助都非常感谢!

以下是我试图实现的一些截图 No Data Loaded Some Data Loaded Download Complete

1 个答案:

答案 0 :(得分:0)

你可以创建一个看起来像这样的WPF UserControl(我会给你留下真正的样式):

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

转换器为:

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是一个int,表示您所在的安装程序中的状态。它也可能是一个枚举。