【问题标题】:WPF DataGrid Children Vertical ScrollingWPF DataGrid子级垂直滚动
【发布时间】:2018-12-16 13:45:31
【问题描述】:

大家好!

我正在进行的项目的第一步已经完成。然而,现在我在 XAML 结构中遇到了一个新问题。

我想弄清楚我希望如何最好地呈现我的数据。目前,我认为使用 DataGrid 是合适的,因为绑定功能强大,并且 DataGrid 会根据我的对象列表适当地填充项目。但是,垂直滚动条似乎只出现在整个行中,而不是数据列中的行 - 而我的第二个数据列可以有一个长文本块!

我对 WPF 的世界还很陌生,所以我很感激任何意见!下面是我的窗口 XAML 代码:

<Window x:Class="_puffDisplay.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:_puffDisplay"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
        <DataGrid Name ="puffCoreView" HorizontalAlignment="Left" Height="400" Margin="10,10,0,0" VerticalAlignment="Top" Width="774" ItemsSource="{Binding}" 
                  IsReadOnly="True"
                  AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header= "Puff #" Width="350" Binding="{Binding PuffNumber}"/>
                <DataGridTextColumn Header="Puff Data" Width="350" Binding="{Binding Data}" ScrollViewer.VerticalScrollBarVisibility="Visible">

                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        </ScrollViewer>

我需要看看如何允许用户在每行的第二列以及整个行内垂直滚动。有什么建议吗?

【问题讨论】:

    标签: c# wpf datagrid scrollviewer vertical-scrolling


    【解决方案1】:

    使用带有 ScrollViewer 和 TextBlock 的 DataGridTemplateColumn 代替 DataGridTextColumn。这是一种可能性...

                <DataGridTemplateColumn Header="Puff Data" Width="350">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ScrollViewer MaxHeight="100">
                                <TextBlock Text="{Binding Data}" TextWrapping="Wrap"/>
                            </ScrollViewer>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
    

    【讨论】:

    • 绝对做到了!非常感谢。
    • 附带问题:有什么建议可以让每个 DataGrid 列单独选择吗?如果您知道我的意思,我希望能够从单个单元格中复制和粘贴数据。
    • 在 DataGrid 上使用 SelectionUnit="Cell"
    • 谢谢你!另外,你看过这个帖子吗? stackoverflow.com/questions/136435/… 我尝试通过“Torvin”实现建议的代码,以使我的文本块中的文本可选择,但到目前为止我没有看到文本突出显示。这是意料之中的吗?
    • @KieranOjakangas 不,我没有看到那个帖子。我不明白。如果您想要可选择的文本,显而易见的方法是使用文本框。
    猜你喜欢
    • 1970-01-01
    • 2012-12-05
    • 2012-07-03
    • 1970-01-01
    • 2017-04-13
    • 2021-04-05
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多