【问题标题】:Issue in vertical scrollbar in silverlight datagridSilverlight 数据网格中的垂直滚动条问题
【发布时间】:2013-04-05 06:44:03
【问题描述】:

我在我的项目中使用 silverlight datatgrid,并在 SelectAll 选项的标题中使用复选框。

垂直滚动数据网格时,标题复选框状态随机变化,但内容复选框状态保持正确。

如果有人遇到这个问题,请帮助我。

<control:DataGrid.RowGroupHeaderStyles>
   <Style TargetType="control:DataGridRowGroupHeader">
         <Setter Property="PropertyNameVisibility" Value="Collapsed" />
         <Setter Property="Background" Value="LightGray" />
         <Setter Property="Foreground" Value="#FF404040" />
         <Setter Property="SublevelIndent" Value="15" />
         <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="control:DataGridRowGroupHeader">
                  <Primitives:DataGridFrozenGrid Name="Root" Background="{TemplateBinding Background}">
                     <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CurrentStates">
                           <VisualState x:Name="Regular"/>
                              <VisualState x:Name="Current">
                               <Storyboard>
                               <DoubleAnimation Storyboard.TargetName="FocusVisual" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                </Storyboard>
                             </VisualState>
                         </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Primitives:DataGridFrozenGrid.Resources>
                          <ControlTemplate x:Key="ToggleButtonTemplate" TargetType="ToggleButton">
                          <Grid Background="Transparent">
                               <VisualStateManager.VisualStateGroups>
                                  <VisualStateGroup x:Name="CommonStates">
                                     <VisualState x:Name="Normal"/>
                                       <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                          <ColorAnimation Storyboard.TargetName="CollapsedArrow" Storyboard.TargetProperty="(Stroke).Color" Duration="0" To="#FF6DBDD1"/>
                                            <ColorAnimation Storyboard.TargetName="ExpandedArrow" Storyboard.TargetProperty="(Fill).Color" Duration="0" To="#FF6DBDD1"/>
                                        </Storyboard>
                                      </VisualState>
                                     <VisualState x:Name="Pressed">
                                       <Storyboard>
                                         <ColorAnimation Storyboard.TargetName="CollapsedArrow" Storyboard.TargetProperty="(Stroke).Color" Duration="0" To="#FF6DBDD1"/>

                                                <Path Stretch="Uniform" Data="F1 M 0,0 L 0,1 L .6,.5 L 0,0 Z" Width="5" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="CollapsedArrow" Visibility="Collapsed" Stroke="#FF414345"/>
                                                <Path Stretch="Uniform" Data="F1 M 0,1 L 1,1 L 1,0 L 0,1 Z" Width="6" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="ExpandedArrow" Fill="#FF414345"/>
                                            </Grid>
                                        </ControlTemplate>
                                    </Primitives:DataGridFrozenGrid.Resources>

                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>

                                    <Rectangle Grid.Column="1" Grid.ColumnSpan="5" Fill="#FFFDD234" Height="1"/>
                                    <Rectangle Grid.Column="1" Grid.Row="1" Name="IndentSpacer" />
                                    <ToggleButton Grid.Column="2" Grid.Row="1" Name="ExpanderButton" Height="15" Width="15" IsTabStop="False" Template="{StaticResource ToggleButtonTemplate}" Margin="2,0,0,0"/>

                                    <StackPanel Grid.Column="3" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" Margin="0,1,0,1">
                                        <CheckBox Tag="{Binding}" Name="headerCheck" Click="headerCheck_Click"  HorizontalAlignment="Left"   />
                                    </StackPanel>

                                    <Rectangle Grid.Column="1" Grid.ColumnSpan="5" Fill="#FF4F54DA" Height="1" Grid.Row="2"/>
                                    <Rectangle Name="FocusVisual" Grid.Column="1" Grid.ColumnSpan="4" Grid.RowSpan="3" Stroke="#FF6DB112" StrokeThickness="1" HorizontalAlignment="Stretch" 
                           VerticalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" />
                                    <Primitives:DataGridRowHeader Name="RowHeader" Grid.RowSpan="3" Primitives:DataGridFrozenGrid.IsFrozen="True" />

                                </Primitives:DataGridFrozenGrid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </control:DataGrid.RowGroupHeaderStyles>

【问题讨论】:

  • 您能否发布有关如何将复选框放在列标题中的代码?
  • 无法在此处发布整个代码。我将解释我的代码中有什么。在 DataGrid.RowGroupHeaderStyles 中,使用 targetType 作为 DataGridRowGroupHeader 创建了样式元素。在此之后为相同的目标类型创建了控制模板。在 controlTemplate 内,可以使用以下代码。
  • 简单的方法是从工具箱中拖动一个复选框,并将其作为全选放入数据网格中。为数据网格中的复选框创建一个模板列,当复选框为复选框时,为复选框添加选中和未选中事件选中将值绑定为选中状态。
  • 但问题不在于复选框事件。每当标题切片键选择时,按预期选择内容组中的复选框。但是在滚动数据网格时,标题检查状态正在发生变化,即,即使我们没有选择标题复选框,它也会被选中。请在上面找到我的代码。

标签: silverlight datagrid


【解决方案1】:

您将标题复选框绑定到当前项目,并且当您滚动时它会发生变化。除非您使用 setter 更改 ItemsSource 集合中的所有值,否则我不会将此复选框绑定到任何内容。在这种情况下,请确保没有其他任何东西会改变它所绑定的属性的值。

【讨论】:

  • 你能举个例子解释一下吗?
【解决方案2】:

这是由于 Silverlight 使用了虚拟化。

我在滚动时遇到了这个问题,网格中的数据在显示方面发生了变化。但实际上是一样的。事实是,在 UI 上,silverlight 显示不正确,因为它在渲染时使用了相同的控件。

最好的方法是在 datagarid 的 LoadingRow 和 UnloadingRow 事件中处理你的逻辑。

我在以下链接上发布了一些详细信息:

https://stackoverflow.com/a/25566163/3989725

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 2015-04-05
    • 2015-07-01
    • 2019-11-08
    相关资源
    最近更新 更多