2013-04-05 140 views
1

我在我的项目中使用Silverlight数据网格,SelectAll选项的标题中有复选框。Silverlight数据网格垂直滚动条中的问题

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

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

<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> 
+0

你能张贴与你怎么把复选框中列的标题的代码? – 2013-04-05 07:33:41

+0

无法在此处发布整个代码。我会解释我的代码中有什么。在DataGrid.RowGroupHeaderStyles中,创建了一个样式元素,其中targetType为DataGridRowGroupHeader。之后,为相同的目标类型创建控件模板。在controlTemplate里面可以使用下面的代码。 2013-04-05 09:02:46

+0

简单的方法是从工具箱中拖出一个复选框,并将其作为select全部放在datagrid中。为datagrid中的复选框添加一个templatecolumn,添加checked和勾选复选框时,复选框未经检查的事件会将所选值绑定到该值。 – Sajeetharan 2013-04-05 09:30:17

回答

0

你必须绑定到当前项目的头复选框,当你滚动它改变。除非您使用setter来更改ItemsSource集合中的所有值,否则我不会将此复选框绑定到任何东西。在这种情况下,确保没有别的东西正在改变它所绑定的属性的值。

+0

能否请你解释一些例子。 – 2013-04-09 05:27:40

0

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

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

最好的方法是处理你的逻辑里面的数据加载的LoadRow和UnloadingRow事件。

我有一些细节上发表了以下链接:

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