2014-07-06 58 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,它表示您所在安裝程序中的哪個狀態。它也可以是一個枚舉。