【问题标题】:Freezing part of a canvas when scrolling in WPF?在 WPF 中滚动时冻结画布的一部分?
【发布时间】:2019-03-13 14:00:42
【问题描述】:

我是 WPF 和 XAML 的新手。我正在研究用于读取 MIDI 文件的钢琴卷帘界面,到目前为止,它看起来像这样: https://i.imgur.com/hBJZhnH.png

我有一个用于整个钢琴卷的画布(围绕 ScrollViewer 进行水平和垂直滚动)。在那个画布中,我有一个用于绘制钢琴音符的grdPiano 网格,一个用于绘制水平行填充颜色的grdGridColours 网格,一个用于绘制垂直网格线的cnvGridLines 画布(这是在 C# 代码中完成的) ,以及一个grdNotes 网格,我在其中用 C# 代码绘制 MIDI 音符。

这很好用,但是当我水平滚动时,钢琴键 (grdPiano) 会滚动到视野之外。我希望这样,如果我垂直滚动,一切都会滚动,但如果我水平滚动,钢琴键会在屏幕左侧的视图中保持冻结状态。我一直在搞乱嵌套的 ScrollViewers,但我似乎无法让它工作。

这是我的 XAML:

<ScrollViewer x:Name="srlPianoScroll"  Margin="125,20,0,0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Canvas x:Name="cnvPianoRoll" HorizontalAlignment="Left" Height="1592" VerticalAlignment="Center" Width="132">
        <Grid x:Name="grdPiano" HorizontalAlignment="Left" Height="1638" VerticalAlignment="Center" Width="132" Canvas.Top="-8">
            <Grid.RowDefinitions>
                <!--Row definitions...-->
            </Grid.RowDefinitions>
            <!--Drawing piano notes...-->
        </Grid>
        <Grid x:Name="grdGridColours" HorizontalAlignment="Left" Height="1628" VerticalAlignment="Center" Width = "0" Canvas.Top="2" Canvas.Left="132" Panel.ZIndex="1">
            <Grid.RowDefinitions>
                <!--Row definitions...-->
            </Grid.RowDefinitions>
            <!--Drawing horizontal fill colours...-->
        </Grid>
        <Canvas x:Name="cnvGridLines" HorizontalAlignment="Left" Height="1592" VerticalAlignment="Center" Width = "0" Canvas.Top="2" Canvas.Left="132" Panel.ZIndex="2"/>
            <!--Vertical grid lines are drawn programatically.-->
        <Grid x:Name="grdNotes" HorizontalAlignment="Left" Height="1628" VerticalAlignment="Center" Width = "0" Canvas.Top="2" Canvas.Left="132" Panel.ZIndex="3">
            <Grid.RowDefinitions>
                <!--Row definitions...-->
            </Grid.RowDefinitions>
        </Grid>
    </Canvas>
</ScrollViewer>

谢谢!

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    尝试将 grdPiano 的 Canvas.Left 绑定到滚动查看器的 Horizo​​ntalOffset。

    当 scrollviewer 水平滚动时,绑定会更新 Canvas.Left 到可见区域的左边缘。如果需要,您可以使用转换器添加偏移量。

    这是一个例子:

    XAML

    <Window x:Class="WpfApp38.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:WpfApp38"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Window.Resources>
            <local:OffsetConverter x:Key="OffsetConverter" />
        </Window.Resources>
        <ScrollViewer Name="sv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <Canvas Width="10600" Height="500">
                <!-- Rectangle that stays on the left side, your grdPiano -->
                <Rectangle Canvas.Left="{Binding ElementName=sv, Path=HorizontalOffset}" Canvas.Top="0" Width="50" Height="500" Fill="Blue" Panel.ZIndex="1" />
    
                <!-- Rectangle that stays on the left side, your grdPiano - - using the OffsetConverter to offset it 300 to the right -->
                <!--<Rectangle Canvas.Left="{Binding ElementName=sv, Path=HorizontalOffset, Converter={StaticResource OffsetConverter}, ConverterParameter=300}" Canvas.Top="0" Width="50" Height="500" Fill="LightBlue" Panel.ZIndex="1" />-->
    
                <!-- Rectangles that scrolls normally -->
                <Rectangle Canvas.Left="150" Canvas.Top="50" Width="200" Height="25" Fill="Black" />
                <Rectangle Canvas.Left="50" Canvas.Top="100" Width="100" Height="25" Fill="Black" />
                <Rectangle Canvas.Left="350" Canvas.Top="200" Width="250" Height="25" Fill="Black" />
                <Rectangle Canvas.Left="600" Canvas.Top="235" Width="10000" Height="25" Fill="Black" />
            </Canvas>
        </ScrollViewer>
    </Window>
    

    以及转换器的代码:

    public class OffsetConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double scrollViewerHorizontalOffset && parameter is string s)
            {
                double offsetAmount;
                double.TryParse(s, out offsetAmount);
    
                return scrollViewerHorizontalOffset + offsetAmount;
            }
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

    • 工作就像一个魅力。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多