StackPanel 在 Datagrid WPF 上停止滚动

StackPanel stop scrolling while over a Datagrid WPF

我一直在研究动态 StackPanel,它实现了 ScrollViewer 中的多个控件。我面临的问题是,当我的鼠标点击 DataGrid 时,我无法滚动浏览整个 StackPanel。我想这是因为 DataGrid 本身有一个滚动选项。我想知道如何禁用它。我更喜欢以编程方式创建和更改控件。在解决方案中,我需要能够修改 DataGrid,并且 DataGrid 不能阻止用户上下滚动。

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

编辑:

编辑:恢复:为了制作一个带有 DataGrid 的流畅 ScrollViewer,您需要向其中添加此函数。

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

编辑:找到解决方案。检查下面的代码!问题是数据网格不会滚动滚动查看器。您获得鼠标滚动事件并滚动滚动查看器。

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