wpf 如何避免在文本框内滚动?

tpgth1q7  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(196)

下面是XAML代码:

<Window
    x:Class="scroll.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:local="clr-namespace:scroll"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="2" VerticalAlignment="Stretch">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <DockPanel
                Grid.Row="1"
                Grid.Column="0"
                Grid.ColumnSpan="2">

                <TabControl Padding="0" TabStripPlacement="Top">
                    <TabItem Header="Receive">
                        <ScrollViewer>
                            <StackPanel Margin="15">
                                <StackPanel
                                    MaxHeight="40"
                                    Margin="0,4,10,0"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Stretch"
                                    Orientation="Horizontal">
                                    <Button
                                        Margin="0,8,18,0"
                                        Background="#349088"
                                        Content="Copy"
                                        FontWeight="ExtraBold"
                                        Foreground="White" />
                                </StackPanel>
                                <Label
                                    Margin="0,0,10,0"
                                    Content="Topic"
                                    FontWeight="Bold" />
                                <TextBox
                                    Margin="0,0,10,0"
                                    FontSize="14"
                                    IsReadOnly="True"
                                    Text="svc/core/state" />
                                <Label
                                    Margin="0,0,10,0"
                                    Content="Header"
                                    FontWeight="Bold" />
                                <Expander Margin="0,0,10,0" Header="This is header! Click me to expand the content.">
                                    <Grid>
                                        <TextBox
                                            Name="ReadOnlyHeaderEditor"
                                            Margin="5,2,10,5"
                                            FontFamily="Consolas, Monaco, Menlo,Ubuntu Mono, Monospace"
                                            FontSize="12"
                                            FontWeight="Normal" />
                                    </Grid>
                                </Expander>
                                <Label
                                    Margin="0,6,10,0"
                                    Content="Payload"
                                    FontWeight="Bold" />
                                <Border
                                    Margin="0,0,10,0"
                                    Background="Transparent"
                                    BorderBrush="#C1D2D2"
                                    BorderThickness="0.7"
                                    CornerRadius="4">
                                    <Grid>
                                        <TextBox
                                            VerticalScrollBarVisibility="Auto"
                                            TextWrapping="Wrap"
                                            Text="BLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlahBLahBlahBlah"
                                            Height="300"
                                            Margin="5,2,10,5"
                                            FontFamily="Consolas, Monaco, Menlo,Ubuntu Mono, Monospace"
                                            FontSize="12"
                                            FontWeight="Normal" />
                                    </Grid>
                                </Border>
                                <Label
                                    Margin="0,6,10,0"
                                    Content="History of received messages"
                                    FontWeight="Bold" />
                            </StackPanel>
                        </ScrollViewer>
                    </TabItem>
                </TabControl>
            </DockPanel>
        </Grid>
    </Grid>
</Window>

我想要的是不允许在一个有很多文本的文本框中滚动,直到一个人将光标放置在文本框中一定的时间。如果光标没有在文本框中设置,我想滚动整个网格,我怎么能做到这一点呢?
我试着做以下事情:尝试处理TextBox的GotFocus和LostFocus以替代光标(可能也是MouseEnter和MouseLeave)。启动计时器。对于滚动传递,尝试处理PreviewMouseWheel并设置e.相应地处理。但我认为这不是正确的方法

6ju8rftf

6ju8rftf1#

你只要取消移动:在ScrollViewer上添加事件ScrollViewer_ScrollChanged

using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp2
{
    public partial class MainWindow : Window
    {
        private bool scrollViewActive = true;
        private int timeTowait = 5000; // en ms
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            scrollViewActive = false;
            await Task.Delay(timeTowait);
            scrollViewActive = true;
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            scrollViewActive = true;
        }

        private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            //cancel the move when scrollview has to be desactivated
            if (!scrollViewActive)
            {
                ScrollViewer scrollViewer = e.OriginalSource as ScrollViewer;
                scrollViewer.ScrollToVerticalOffset(0);
                return;
            }
        }
    }
}

相关问题