2016-04-27 11 views
2

Ich habe auf einem dynamischen Stackpanel arbeiten, die mehrere Steuerelemente implementieren, die in einem Scrollviewer ist. Das Problem, das mir gegenübersteht, ist, dass ich nicht durch mein gesamtes StackPanel blättern kann, wenn meine Maus ein DataGrid trifft. Ich dachte, dass das daran liegt, dass ein DataGrid selbst eine Scroll-Option hat. Ich möchte wissen, wie ich es deaktivieren kann. Ich bevorzuge es, meine Steuerelemente programmgesteuert zu erstellen und zu ändern. In der Lösung muss ich in der Lage sein, das DataGrid zu ändern, und das DataGrid darf den Benutzer nicht daran hindern, nach unten und oben zu scrollen.Stackpanel aufhören zu scrollen, während sie über eine Datagrid WPF

<Window x:Class="testscroll.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:testscroll" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 

    <ScrollViewer HorizontalAlignment="Left" Height="300" Margin="10,10,0,0"VerticalAlignment="Top" Width="497"> 

     <StackPanel x:Name="panel" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" /> 
    </ScrollViewer> 

</Grid> 

Dies ist mein Testcode

using System.Data; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 


namespace testscroll 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      panel.Children.Clear(); 


      Label lblTicket = new Label(); 
      lblTicket.Content = "RT Ticket"; 
      panel.Children.Add(lblTicket); 
      TextBox txtBoxRTTicket = new TextBox(); 

      txtBoxRTTicket.Text = "test"; 
      txtBoxRTTicket.MaxLength = 5; 
      txtBoxRTTicket.Margin = new Thickness(60, -25, 0, 0); 
      panel.Children.Add(txtBoxRTTicket); 

      TextBox txtBoxRTTicketFull = new TextBox(); 
      txtBoxRTTicketFull.Foreground = Brushes.Blue; 
      txtBoxRTTicketFull.TextDecorations = TextDecorations.Underline; 
      txtBoxRTTicketFull.Name = "txtBoxRTTicketFull"; 
      txtBoxRTTicketFull.Text = "http://mtl-ppapp-rt01/rt/Ticket/Display.html?id=" + "11111"; 
      //txtBoxRTTicket.TextChanged += TxtBoxRTTicket_TextChanged; 
      //txtBoxRTTicketFull.MouseDoubleClick += TextBox_MouseDoubleClick; 
      txtBoxRTTicketFull.IsReadOnly = true; 
      txtBoxRTTicketFull.Margin = new Thickness(200, -25, 0, 0); 
      panel.Children.Add(txtBoxRTTicketFull); 

      Label space1 = new Label(); 
      space1.Content = ""; 
      panel.Children.Add(space1); 
      Label lblTrackPage = new Label(); 
      lblTrackPage.Content = "Trac Page"; 
      panel.Children.Add(lblTrackPage); 
      TextBox txtBoxTrackPage = new TextBox(); 
      txtBoxTrackPage.Text = "test"; 
      txtBoxTrackPage.Foreground = Brushes.Blue; 
      txtBoxTrackPage.TextDecorations = TextDecorations.Underline; 
      //txtBoxTrackPage.MouseDoubleClick += TextBox_MouseDoubleClick; 
      txtBoxTrackPage.Margin = new Thickness(60, -25, 0, 0); 
      panel.Children.Add(txtBoxTrackPage); 
      //un espace dans le stackpanel 
      Label space2 = new Label(); 
      space2.Content = ""; 
      panel.Children.Add(space2); 
      //on ajoute le premier tableau concernant le pcb 
      DataTable tablePCB = new DataTable(); 
      tablePCB.Columns.Add("PCB Number"); 
      tablePCB.Columns.Add("CAD Number"); 
      tablePCB.Columns.Add("Baan Descriptiion"); 
      tablePCB.Columns.Add("Display Description"); 
      tablePCB.Columns.Add("Detail Description"); 
      tablePCB.Rows.Add(); 
      tablePCB.Rows[0][0] = "000-00000-00"; 
      string cadNbr = ((string)tablePCB.Rows[0][0]).Substring(3, 6); 
      tablePCB.Rows[0][1] = "cad" + cadNbr; 
      tablePCB.Rows[0][2] = "test"; 
      tablePCB.Rows[0][3] = "test"; 
      tablePCB.Rows[0][4] = "test"; 
      tablePCB.Columns[0].ReadOnly = true; 
      tablePCB.Columns[1].ReadOnly = true; 
      tablePCB.Columns[2].ReadOnly = true; 
      tablePCB.Columns[3].ReadOnly = true; 
      DataGrid dgvPCB = new DataGrid(); 

      dgvPCB.CanUserAddRows = false; 
      dgvPCB.ItemsSource = tablePCB.DefaultView; 
      panel.Children.Add(dgvPCB); 
      //un espace dans le stackpanel 
      Label space3 = new Label(); 
      space3.Content = ""; 
      panel.Children.Add(space3); 
      //on ajoute le premier tableau concernant les assembly 
      DataTable tableAssy = new DataTable(); 
      tableAssy.Columns.Add("Assembly Number"); 
      tableAssy.Columns.Add("Schematic Description"); 
      tableAssy.Columns.Add("Work Instruction"); 
      tableAssy.Columns.Add("Warehouse"); 
      tableAssy.Columns.Add("Baan Description");//isreadonly 
      tableAssy.Columns.Add("Display Description"); 
      tableAssy.Columns.Add("Detail Description"); 
      for (int i = 0; i < 1; i++) 
      { 
       tableAssy.Rows.Add(); 
       tableAssy.Rows[i][0] = "test"; 
       tableAssy.Rows[i][1] = "test"; 
       tableAssy.Rows[i][2] = "test"; 
       tableAssy.Rows[i][3] = "test"; 
       tableAssy.Rows[i][4] = "test"; 
       tableAssy.Rows[i][5] = "test"; 
       tableAssy.Rows[i][6] = "test"; 
      } 
      tableAssy.Columns[3].ReadOnly = true; 
      tableAssy.Columns[4].ReadOnly = true; 
      DataGrid dgvAssy = new DataGrid(); 
      //dgvAssy.CanUserAddRows = false; 
      dgvAssy.ItemsSource = tableAssy.DefaultView; 

      panel.Children.Add(dgvAssy); 

      if (null != panel.FindName(txtBoxRTTicketFull.Name)) 
       panel.UnregisterName(txtBoxRTTicketFull.Name); 
      panel.RegisterName(txtBoxRTTicketFull.Name, txtBoxRTTicketFull); 

      Label lblNote = new Label(); 
      lblNote.Content = "Notes:"; 

      TextBox txtBoxNotes = new TextBox(); 
      txtBoxNotes.Text = "test"; 
      txtBoxNotes.AcceptsReturn = true; 
      txtBoxNotes.Height = 300; 
      txtBoxNotes.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; 
      txtBoxNotes.MaxHeight = 300; 
      txtBoxNotes.TextWrapping = TextWrapping.Wrap; 
      txtBoxNotes.MaxWidth = 800; 
      txtBoxNotes.ScrollToEnd(); 
      Button btnAddAssembly = new Button(); 
      btnAddAssembly.Content = "Save changes"; 
      //btnAddAssembly.Click += ((sender1, e1) => save_click(sender1, e1, txtBoxRTTicket.Text, txtBoxTrackPage.Text, (string)tablePCB.Rows[0][3], (string)tablePCB.Rows[0][4], tableAssy, txtBoxNotes, log)); 
      panel.Children.Add(btnAddAssembly); 

      panel.Children.Add(lblNote); 

      panel.Children.Add(txtBoxNotes); 
     } 

    } 
} 

EDIT: enter image description here

+1

Sie müssen 1 WPF Lektion übersprungen; Erstellen Sie Ihre Ansicht mit XAML –

+0

Warum kein XAML. Wenn Sie es während der Kompilierung generieren, ist XAML sowohl einfacher zu schreiben als auch besser zu sehen. –

+0

Da die Stackpanel Änderung als Benutzer Wert eingibt oder die Auswahl bekommen einige Teile der Platte ändern entfernt und andere hinzugefügt, die es nicht möglich, es in XAML zu codieren. –

Antwort

1

EDIT: FORTGESETZT: Um einen Scroll zu machen, die mit Datagrid in dem Fluid ist, Sie muss diese Funktion hinzufügen.

DataGrid dgvAssy = new DataGrid(); 

dgv.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; 

dgv.PreviewMouseWheel += DgvAssy_PreviewMouseWheel; 


private void DgvAssy_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) 
{ 
    scrollviewer1.ScrollToVerticalOffset(scrollviewer1.VerticalOffset - e.Delta); 
} 

EDIT: Die Lösung gefunden. Überprüfen Sie den Code unten! Das Problem besteht darin, dass das Datagrid den Bildlauf-Viewer nicht durchläuft. Sie erhalten das Maus-Scroll-Ereignis und scrollen den Scroll-Viewer.

XAML:

<Window x:Class="testscroll.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:testscroll" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ScrollViewer x:Name="scrollviewer1" HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" VerticalAlignment="Top" Width="497"> 
      <StackPanel x:Name="panel" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" /> 
     </ScrollViewer> 
    </Grid> 
</Window> 

-Code hinter:

using System.Data; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 


namespace testscroll 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      panel.Children.Clear(); 


      Label lblTicket = new Label(); 
      lblTicket.Content = "RT Ticket"; 
      panel.Children.Add(lblTicket); 
      TextBox txtBoxRTTicket = new TextBox(); 

      txtBoxRTTicket.Text = "test"; 
      txtBoxRTTicket.MaxLength = 5; 
      txtBoxRTTicket.Margin = new Thickness(60, -25, 0, 0); 
      panel.Children.Add(txtBoxRTTicket); 

      TextBox txtBoxRTTicketFull = new TextBox(); 
      txtBoxRTTicketFull.Foreground = Brushes.Blue; 
      txtBoxRTTicketFull.TextDecorations = TextDecorations.Underline; 
      txtBoxRTTicketFull.Name = "txtBoxRTTicketFull"; 
      txtBoxRTTicketFull.Text = "http://mtl-ppapp-rt01/rt/Ticket/Display.html?id=" + "11111"; 
      //txtBoxRTTicket.TextChanged += TxtBoxRTTicket_TextChanged; 
      //txtBoxRTTicketFull.MouseDoubleClick += TextBox_MouseDoubleClick; 
      txtBoxRTTicketFull.IsReadOnly = true; 
      txtBoxRTTicketFull.Margin = new Thickness(200, -25, 0, 0); 
      panel.Children.Add(txtBoxRTTicketFull); 

      Label space1 = new Label(); 
      space1.Content = ""; 
      panel.Children.Add(space1); 
      Label lblTrackPage = new Label(); 
      lblTrackPage.Content = "Trac Page"; 
      panel.Children.Add(lblTrackPage); 
      TextBox txtBoxTrackPage = new TextBox(); 
      txtBoxTrackPage.Text = "test"; 
      txtBoxTrackPage.Foreground = Brushes.Blue; 
      txtBoxTrackPage.TextDecorations = TextDecorations.Underline; 
      //txtBoxTrackPage.MouseDoubleClick += TextBox_MouseDoubleClick; 
      txtBoxTrackPage.Margin = new Thickness(60, -25, 0, 0); 
      panel.Children.Add(txtBoxTrackPage); 
      //un espace dans le stackpanel 
      Label space2 = new Label(); 
      space2.Content = ""; 
      panel.Children.Add(space2); 
      //on ajoute le premier tableau concernant le pcb 
      DataTable tablePCB = new DataTable(); 
      tablePCB.Columns.Add("PCB Number"); 
      tablePCB.Columns.Add("CAD Number"); 
      tablePCB.Columns.Add("Baan Descriptiion"); 
      tablePCB.Columns.Add("Display Description"); 
      tablePCB.Columns.Add("Detail Description"); 
      tablePCB.Rows.Add(); 
      tablePCB.Rows[0][0] = "000-00000-00"; 
      string cadNbr = ((string)tablePCB.Rows[0][0]).Substring(3, 6); 
      tablePCB.Rows[0][1] = "cad" + cadNbr; 
      tablePCB.Rows[0][2] = "test"; 
      tablePCB.Rows[0][3] = "test"; 
      tablePCB.Rows[0][4] = "test"; 
      tablePCB.Columns[0].ReadOnly = true; 
      tablePCB.Columns[1].ReadOnly = true; 
      tablePCB.Columns[2].ReadOnly = true; 
      tablePCB.Columns[3].ReadOnly = true; 
      DataGrid dgvPCB = new DataGrid(); 

      dgvPCB.CanUserAddRows = false; 
      dgvPCB.ItemsSource = tablePCB.DefaultView; 

      panel.Children.Add(dgvPCB); 
      //un espace dans le stackpanel 
      Label space3 = new Label(); 
      space3.Content = ""; 
      panel.Children.Add(space3); 
      //on ajoute le premier tableau concernant les assembly 
      DataTable tableAssy = new DataTable(); 
      tableAssy.Columns.Add("Assembly Number"); 
      tableAssy.Columns.Add("Schematic Description"); 
      tableAssy.Columns.Add("Work Instruction"); 
      tableAssy.Columns.Add("Warehouse"); 
      tableAssy.Columns.Add("Baan Description");//isreadonly 
      tableAssy.Columns.Add("Display Description"); 
      tableAssy.Columns.Add("Detail Description"); 
      for (int i = 0; i < 1; i++) 
      { 
       tableAssy.Rows.Add(); 
       tableAssy.Rows[i][0] = "test"; 
       tableAssy.Rows[i][1] = "test"; 
       tableAssy.Rows[i][2] = "test"; 
       tableAssy.Rows[i][3] = "test"; 
       tableAssy.Rows[i][4] = "test"; 
       tableAssy.Rows[i][5] = "test"; 
       tableAssy.Rows[i][6] = "test"; 
      } 
      tableAssy.Columns[3].ReadOnly = true; 
      tableAssy.Columns[4].ReadOnly = true; 
      DataGrid dgvAssy = new DataGrid(); 
      dgvPCB.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; 
      dgvAssy.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; 

      dgvAssy.PreviewMouseWheel += DgvAssy_PreviewMouseWheel; 
      dgvPCB.PreviewMouseWheel += DgvAssy_PreviewMouseWheel; 
      //dgvAssy.CanUserAddRows = false; 
      dgvAssy.ItemsSource = tableAssy.DefaultView; 

      panel.Children.Add(dgvAssy); 

      if (null != panel.FindName(txtBoxRTTicketFull.Name)) 
       panel.UnregisterName(txtBoxRTTicketFull.Name); 
      panel.RegisterName(txtBoxRTTicketFull.Name, txtBoxRTTicketFull); 

      Label lblNote = new Label(); 
      lblNote.Content = "Notes:"; 

      TextBox txtBoxNotes = new TextBox(); 
      txtBoxNotes.Text = "test"; 
      txtBoxNotes.AcceptsReturn = true; 
      txtBoxNotes.Height = 300; 
      txtBoxNotes.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; 
      txtBoxNotes.MaxHeight = 300; 
      txtBoxNotes.TextWrapping = TextWrapping.Wrap; 
      txtBoxNotes.MaxWidth = 800; 
      txtBoxNotes.ScrollToEnd(); 
      Button btnAddAssembly = new Button(); 
      btnAddAssembly.Content = "Save changes"; 
      //btnAddAssembly.Click += ((sender1, e1) => save_click(sender1, e1, txtBoxRTTicket.Text, txtBoxTrackPage.Text, (string)tablePCB.Rows[0][3], (string)tablePCB.Rows[0][4], tableAssy, txtBoxNotes, log)); 
      panel.Children.Add(btnAddAssembly); 

      panel.Children.Add(lblNote); 

      panel.Children.Add(txtBoxNotes); 

     } 

     private void DgvAssy_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) 
     { 
      scrollviewer1.ScrollToVerticalOffset(scrollviewer1.VerticalOffset - e.Delta); 
     } 
    } 
} 
+0

HorizontalScrollBarVisibilityProperty nicht in WPF existieren , was Sie mir vorschlagen, ist: myDataGrid.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; Aber das möchte ich nicht erreichen. Ich möchte das Scrollen Ereignis über das Gitter entfernen, wenn die Maus über das Gitter vorbei ist, wenn ich meine Scroll verschiebe –

+0

Wer der HorizontalScrollBarVisibilityProperty sagte in WPF nicht verfügbar ist? Es ist für einen Datagrid zur Verfügung. Ihre Frage war, wie Sie das Scrollen für Datagrid deaktivieren können, richtig? – ViVi

+0

Ich habe eine Bearbeitung vorgenommen, um Ihnen zu zeigen, dass die Settings-Eigenschaft "HorizontalScrollBarVisibilityProperty" nicht verfügbar ist. Aber das ist nicht meine Frage. Ich möchte die Sichtbarkeit der Bildlaufleiste nicht ausblenden. Ich möchte nicht, dass mein Raster das ScrollViewer-Scrollen beeinträchtigt. Wenn Sie meinen Code ausprobieren und Ihre Maus über ein DataGrid legen, können Sie den scrollViewer nicht scrollen. Ich möchte nicht, dass das passiert. –