【问题标题】:Itemscontrol Scrollviewer not working with dataItemscontrol Scrollviewer 不处理数据
【发布时间】:2019-03-19 23:44:54
【问题描述】:

我已经到处搜索了,即使每个人似乎都有这个问题,但我找不到针对我的特定问题的解决方案..

这就是问题所在。 我想制作一个自定义日历控件。为了做到这一点,我用 TextBlocks 填充 ItemsControl,然后在它周围放置一个滚动查看器。

但由于某种原因,scrollviewer 滚动条似乎已禁用,并且似乎无法识别它已填充数据。

这是我的代码

                   <Grid>          
                       <ScrollViewer>
                            <ItemsControl ItemsSource="{Binding CalendarDates}" Height="75">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate DataType="local:Calender">
                                            <TextBlock Name="CalendarDate" FontSize="12" Text="{Binding}" TextAlignment="Right" VerticalAlignment="Top" Height="Auto"/>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <UniformGrid Rows="1" Columns="7"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                        </ScrollViewer>
                    </Grid>

这是我初始化它的 MainWindow.xaml

    <Grid>
    <!--Row Definitins -->
    <Grid.RowDefinitions>
        <RowDefinition Height = "Auto"/>
        <RowDefinition Height = "*"/>
        <RowDefinition Height = "Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="25*"/>
        <ColumnDefinition Width="10*"/>
    </Grid.ColumnDefinitions>

<localControl:Calender Grid.Column="1" Grid.Row="1"/>

</Grid>

代码很好地填充了滚动查看器,但就像我在上面所说的,滚动条似乎已禁用,即使我对大小进行硬编码,它仍然无法正常工作!

此外,我已经尝试设置 SccrollViewer.VerticalScrollBar= Visible 和滚动查看器的高度,以及 Stack Overflow 上的十几个“修复”,但在我的情况下它们都不起作用

【问题讨论】:

  • “滚动条”是水平滚动条吗?
  • 不,Scrollviewer 上的垂直滚动条。它是可见的,它似乎不适用于我输入的数据。相信我,我已经检查了你所有的问题,这不是其中任何一个的重复。

标签: wpf xaml scrollviewer


【解决方案1】:

我找到了答案... ItemsControl 模板本身不允许使用滚动查看器。

我在这本杂志第 38 页找到了答案。

https://dncmagazine.blob.core.windows.net/edition20/DNCMag-Issue20.pdf

我们需要修改ItemsControl模板如下:

 <ItemsControl.Template>  
 <ControlTemplate TargetType=  "ItemsControl">    
 <ScrollViewer>      
 <ItemsPresenter/>    
 </ScrollViewer>  
 </ControlTemplate>
 </ItemsControl.Template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多