2016-04-27 64 views
2

我一直在研究一個動態的StackPanel,它實現了ScrollViewer中的多個控件。我面臨的問題是,當鼠標碰到DataGrid時,我無法滾動整個StackPanel。我想這是因爲DataGrid本身有一個滾動選項。我想知道如何禁用它。我更喜歡以編程方式創建和更改我的控件。在解決方案中,我需要能夠修改DataGrid,並且DataGrid不能阻止用戶向下滾動。StackPanel通過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> 

這是我的測試代碼

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

    } 
} 

編輯: enter image description here

+1

您必須跳過WPF第一課;使用XAML創建您的視圖 –

+0

爲什麼沒有XAML。如果您在編譯期間生成它,則XAML更易於編寫和查看。 –

+0

由於StackPanel在用戶輸入值或更改選擇時發生更改,因此面板的某些部分將被刪除,而其他部分將被添加,這使得無法在XAML中對其進行編碼。 –

回答

1

編輯:續:爲了使一個ScrollViewer中是流體用的DataGrid中,你需要將此功能添加到它。

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

編輯:找到解決方案。檢查下面的代碼!問題是datagrid不滾動滾動查看器。您將獲得鼠標滾動事件並滾動滾動查看器。

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> 

背後代碼:

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

Horizo​​ntalScrollBarVisibilityProperty在WPF中不存在 您對我的建議是: myDataGrid.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; 但那不是我想要達到的。我想在滾動我的ScrollViewer時將鼠標移過網格時移除網格上的滾動事件 –

+0

誰告訴Horizo​​ntalScrollBarVisibilityProperty在WPF中不可用? 它可用於數據網格。你的問題是關於如何禁用數據網格的滾動,對嗎? – ViVi

+0

我做了一個編輯,告訴你Horizo​​ntalScrollBarVisibilityProperty不能被SetValue訪問。但這不是我的問題。我不想隱藏滾動條的可見性。我不希望我的網格不干擾ScrollViewer的滾動。如果您嘗試我的代碼並將鼠標放在DataGrid上,則無法滾動scrollViewer。我不希望發生這種情況。 –