2017-05-12 2 views
0

BEARBEITEN POST:Dynamische Fortschrittsbalken in WPF

Ich brauche eine Fortschrittsanzeige in WPF. Das ProgressBar.Maximum sollte dynamisch sein und die Anzahl der Zeilen von DataGrid enthalten. Ich möchte meinen Fortschrittsbalken nach meiner Funktion CreationBT();

Ist ein BackGroundWorker der gute Weg zu tun, was ich will?

Hier ist mein Code:

Namespace VirtualBOUIN {

public static class DataGridExtensions 
{ 
    public static T GetVisualChild<T>(Visual parent) where T : Visual 
    { 
     T child = default(T); 
     int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
     for (int i = 0; i < numVisuals; i++) 
     { 
      Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); 
      child = v as T; 
      if (child == null) 
      { 
       child = GetVisualChild<T>(v); 
      } 
      if (child != null) 
      { 
       break; 
      } 
     } 
     return child; 
    } 
    public static DataGridRow GetSelectedRow(this DataGrid grid) 
    { 
     return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem); 
    } 
    public static DataGridRow GetRow(this DataGrid grid, int index) 
    { 
     DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index); 
     if (row == null) 
     { 
      // May be virtualized, bring into view and try again. 
      grid.UpdateLayout(); 
      grid.ScrollIntoView(grid.Items[index]); 
      row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index); 
     } 
     return row; 
    } 
    public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column) 
    { 
     if (row != null) 
     { 
      DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row); 

      if (presenter == null) 
      { 
       grid.ScrollIntoView(row, grid.Columns[column]); 
       presenter = GetVisualChild<DataGridCellsPresenter>(row); 
      } 

      DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
      return cell; 
     } 
     return null; 
    } 
    public static DataGridCell GetCell(this DataGrid grid, int row, int column) 
    { 
     DataGridRow rowContainer = grid.GetRow(row); 
     return grid.GetCell(rowContainer, column); 
    } 
} 


public static class SendKeys 
{ 
    /// <summary> 
    /// Sends the specified key. 
    /// </summary> 
    /// <param name="key">The key.</param> 
    public static void Send(Key key) 
    { 
     if (Keyboard.PrimaryDevice != null) 
     { 
      if (Keyboard.PrimaryDevice.ActiveSource != null) 
      { 
       var e1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Down) { RoutedEvent = Keyboard.KeyDownEvent }; 
       InputManager.Current.ProcessInput(e1); 
      } 
     } 
    } 
} 


public partial class MainWindow : Window 
{ 
    BackgroundWorker worker = new BackgroundWorker(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    static extern bool SetCursorPos(int x, int y); 

    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); 

    public const int MOUSEEVENTF_LEFTDOWN = 0x02; 
    public const int MOUSEEVENTF_LEFTUP = 0x04; 



    public static void LeftMouseClick(int xpos, int ypos) 
    { 
     SetCursorPos(xpos, ypos); 
     mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0); 
     mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0); 
    } 

    public struct Ligne 
    { 
     public string Rubrique { set; get; } 
     public string Equipement { set; get; } 
     public string Etat { set; get; } 
     public string Organe { set; get; } 
     public string Etat_Org { set; get; } 
     public string Destinataire { set; get; } 
     public string Intervenant { set; get; } 
     public string Commentaire { set; get; } 
     public string Date_Inter { set; get; } 
     public string Heure { set; get; } 
     public string Diag { set; get; } 
     public string Remede { set; get; } 
     public string Temps { set; get; } 
     public string Obs { set; get; } 
    } 

    private void remplir_data() 
    { 

     OpenFileDialog fichier = new OpenFileDialog(); 
     if (fichier.ShowDialog() == true) // Test result. 
     { 
      string file = fichier.FileName; 

      if (file.Contains(".txt")) 
      { 
       StreamReader sr = new StreamReader(file); 
       while (!sr.EndOfStream) 
       { 
        string maLigne = sr.ReadLine(); 
        DGV.Items.Add(new Ligne 
        { 
         Rubrique = maLigne.Split(';')[0], 
         Equipement = maLigne.Split(';')[1], 
         Etat = maLigne.Split(';')[2], 
         Organe = maLigne.Split(';')[3], 
         Etat_Org = maLigne.Split(';')[4], 
         Destinataire = maLigne.Split(';')[5], 
         Intervenant = maLigne.Split(';')[6], 
         Commentaire = maLigne.Split(';')[7], 
         Date_Inter = maLigne.Split(';')[8], 
         Heure = maLigne.Split(';')[9], 
         Diag = maLigne.Split(';')[10], 
         Remede = maLigne.Split(';')[11], 
         Temps = maLigne.Split(';')[12], 
         Obs = maLigne.Split(';')[13], 
        }); 
       } 
       sr.Close(); 
      } 
      else MessageBox.Show("Selectionnez un fichier compatible"); 
     } 

    } 

    private void BT_REMPLIRDATA_Click(object sender, EventArgs e) 
    { 
     this.DGV.Items.Clear(); 
     remplir_data(); 
    } 

    private void BT_QUITTER_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    public void Creation_BT(int i) 
    { 
     int x = 150; 
     int y = 236; 

     DataGridCell dataGridCell = null; 

     LeftMouseClick(x, y); 
     dataGridCell = this.DGV.GetCell(i, 0); 
     TextBlock cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 25); 
     dataGridCell = DGV.GetCell(i, 1); 
     cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 45); 
     dataGridCell = DGV.GetCell(i, 2); 
     cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 65); 
     dataGridCell = DGV.GetCell(i, 3); 
     cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 85); 
     System.Threading.Thread.Sleep(300); 
     dataGridCell = DGV.GetCell(i, 4); 
     cell = dataGridCell.Content as TextBlock; 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 175); 
     System.Threading.Thread.Sleep(300); 
     dataGridCell = DGV.GetCell(i, 5); 
     cell = dataGridCell.Content as TextBlock; 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 200); 
     System.Threading.Thread.Sleep(300); 
     dataGridCell = DGV.GetCell(i, 6); 
     cell = dataGridCell.Content as TextBlock; 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 255); 
     System.Threading.Thread.Sleep(300); 
     dataGridCell = DGV.GetCell(i, 7); 
     cell = dataGridCell.Content as TextBlock; 
     InputSimulator.SimulateTextEntry(cell.Text); 

     System.Threading.Thread.Sleep(300); 
     LeftMouseClick(x, y - 50); 

     LeftMouseClick(x, y); 
     System.Threading.Thread.Sleep(300); 
     dataGridCell = DGV.GetCell(i, 8); 
     cell = dataGridCell.Content as TextBlock; 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 100); 
     System.Threading.Thread.Sleep(300); 
     dataGridCell = DGV.GetCell(i, 8); 
     cell = dataGridCell.Content as TextBlock; 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x + 60, y + 100); 
     System.Threading.Thread.Sleep(300); 
     dataGridCell = DGV.GetCell(i, 9); 
     cell = dataGridCell.Content as TextBlock; 
     InputSimulator.SimulateTextEntry(cell.Text); 

     System.Threading.Thread.Sleep(300); 
     LeftMouseClick(100, 580); 
     System.Threading.Thread.Sleep(500); 

     LeftMouseClick(x + 50, y - 100); 
     System.Threading.Thread.Sleep(300); 
     LeftMouseClick(x, y - 100); 
     System.Threading.Thread.Sleep(500); 
     LeftMouseClick(x, y - 100); 
     InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL); 
     InputSimulator.SimulateKeyDown(VirtualKeyCode.VK_C); 

     System.Threading.Thread.Sleep(300); 

     InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL); 
     InputSimulator.SimulateKeyUp(VirtualKeyCode.VK_C); 
    } 

    private void Creation_ACTIVITE(int i, string BT) 
    { 
     int x = 150; 
     int y = 236; 
     DataGridCell dataGridCell = null; 

     LeftMouseClick(x + 250, y - 150); 
     System.Threading.Thread.Sleep(300); 

     LeftMouseClick(x, y - 20); 
     InputSimulator.SimulateTextEntry(BT); 

     LeftMouseClick(x, y + 100); 
     dataGridCell = DGV.GetCell(i, 10); 
     TextBlock cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 120); 
     dataGridCell = DGV.GetCell(i, 11); 
     cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 190); 
     dataGridCell = DGV.GetCell(i, 8); 
     cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x + 70, y + 190); 
     dataGridCell = DGV.GetCell(i, 9); 
     cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x + 270, y + 190); 
     dataGridCell = DGV.GetCell(i, 12); 
     cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x, y + 240); 
     dataGridCell = DGV.GetCell(i, 13); 
     cell = dataGridCell.Content as TextBlock; 
     System.Threading.Thread.Sleep(300); 
     InputSimulator.SimulateTextEntry(cell.Text); 

     LeftMouseClick(x - 100, y + 300); 

     System.Threading.Thread.Sleep(500); 
     LeftMouseClick(x + 150, y + 300); 

     System.Threading.Thread.Sleep(500); 
     LeftMouseClick(x + 330, y + 370); 

    } 

    public void BT_REMPLIRBT_Click(object sender, RoutedEventArgs e) 
    { 

     int nbligne = DGV.Items.Count - 1; 
     PGB.Maximum = nbligne; 
     for (int i = 0; i <= nbligne; i++) 
     { 
      string BT = null; 
      Creation_BT(i); 
      // Need here to increment my progress bar 
      System.Threading.Thread.Sleep(1000);     

      IDataObject ClipData = Clipboard.GetDataObject(); 

      if (ClipData.GetDataPresent(DataFormats.Text)) 
      { 
       BT = Clipboard.GetData(DataFormats.Text).ToString(); 
       File.AppendAllText(@"D:\test.txt", BT + "\r\n"); 
      } 

      Creation_ACTIVITE(i, BT); 
      // Need here to increment my progress bar 

      System.Threading.Thread.Sleep(1000); 
     } 
     // Need here to complete my progress bar 
    } 
+0

Normalerweise, wenn Sie dies debuggen, würde es Ihnen zeigen, bei welcher Linie seine Erroring, und spezifischer sein. – BugFinder

+0

Siehe markiert duplizieren für eine angemessene Diskussion des Problems. Beachten Sie, dass in Ihrem Fall, soweit der Zugriff auf die 'ProgressBar' geht, einfach der maximale Wert _before_ beim Starten des Worker gesetzt wird. Es gibt nicht genügend Kontext in Ihrem Beitrag, um bessere Möglichkeiten für den Umgang mit dem 'DataGridView' vorzuschlagen. Zumindest können Sie 'Dispatcher.Invoke()' verwenden, aber es ist möglich, dass es andere MVVM-freundliche Ansätze gibt, abhängig davon, wie der Rest Ihres Codes implementiert ist. –

+0

Alle Operationen mit Datagridview sind in Function CreationBT (i); 'code public void Erstellung_BT (int i) { int x = 150; int y = 236; DataGridCell dataGridCell = null; LeftMouseClick (x, y); dataGridCell = this.DGV.GetCell (i, 0); TextBlock Zelle = dataGridCell.Content als TextBlock; System.Threading.Thread.Sleep (300); InputSimulator.SimulateTextEntry (cell.Text); ....... 'und so ist das Problem bei DGV genauso wie bei PGB – beetS

Antwort

0

DoWork wird in einem anderen Thread arbeitet, so dass Sie nicht die Bedienelemente von hier aus direkt zugreifen soll. Sie haben wahrscheinlich die Ausnahme von dieser Linie erhalten:

PGB.Maximum = max; 

das Maximum einstellen, bevor Sie den Hintergrundjob starten oder verwenden Sie aufrufen, um den Betrieb in UI-Thread Marshal:

// In Windows Forms: 
Invoke(new Action(() => { PGB.Maximum = max; })); 

// In WPF: 
Dispatcher.Invoke(() => PGB.Maximum = max) 

ReportProgress ist eine integrierte Unterstützung zu vermeide dieses Marshalling. Das Ereignis ProgressChanged wird im UI-Thread aufgerufen, sodass Sie den Fortschrittsbalken hier direkt aktualisieren können.

Update: Jetzt sehe ich, dass Sie WPF verwenden, also habe ich meine Antwort bearbeitet.