2014-07-06 40 views
2

我发现一个程序,并试图将它从C#移植到WPF,即时通讯做得很好,但我有UserControl的问题。我无法将其转换为WPF,因为我的知识是有限的。在C#中,我们使用的OnPaint,在WPF的OnRender,我不能得到一个线索,如何实现在WPF 这里同用户控件的代码:WPF移植的WinForms

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中也没有关于WPF OnRender的这种选项,但仍然不知道如何执行此操作。任何帮助真的很感激!

这里是什么即时试图实现 No Data Loaded Some Data Loaded Download Complete

+3

'C#to WPF'这是什么意思? – EZI

+0

@EZI LOL ...我认为OP意味着WPF的winforms。 –

+0

是的,编辑的主题标题 –

回答

0

您可以创建一个WPF用户控件,看起来像这样的一些截图(我将离开实际的造型由你):

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