2009-06-26 78 views
12

当用户点击DataGrid中的行(或使用键盘)选中整个行,该行被选中,但他们点击特定小区也给出了自己的特别关注。这对于数据编辑网格来说很好,但我试图创建更类似于显示列表中每个项的属性的东西,所以...只在Silverlight的DataGrid

是否可以配置(只读)DataGrid用户只能选择或关注整行,而不是单个字段。

如果这是不可能的,是否有一种优雅的方式可以选择第一个元素 - 例如在标准的Windows Open对话框中,如果更改为Details视图,则每行有几列(Filename,Created Date ,大小等),但只能突出显示文件名列中的项目。

回答

8

这里是我的(跛脚)版本,增加了当前行选定的行和灰色的背景上的黑色背景。我不得不覆盖风格,因为我是单独画的细胞和选择的行是隐藏的。

只需粘贴代码添加到您DataGrid实例:

 <local:DataGrid.RowStyle> 
      <Style TargetType="local:DataGridRow"> 
       <Setter Property="IsTabStop" Value="False" /> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="local:DataGridRow"> 
          <localprimitives:DataGridFrozenGrid Name="Root"> 
           <vsm:VisualStateManager.VisualStateGroups> 
            <vsm:VisualStateGroup x:Name="CommonStates"> 
             <vsm:VisualStateGroup.Transitions> 
              <vsm:VisualTransition GeneratedDuration="0" /> 
             </vsm:VisualStateGroup.Transitions> 
             <vsm:VisualState x:Name="Normal" /> 
             <vsm:VisualState x:Name="Normal AlternatingRow"> 
              <Storyboard> 
               <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="0"/> 
              </Storyboard> 
             </vsm:VisualState> 
             <vsm:VisualState x:Name="MouseOver"> 
              <Storyboard> 
               <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To=".5"/> 
              </Storyboard> 
             </vsm:VisualState> 
             <vsm:VisualState x:Name="Normal Selected"> 
              <Storyboard> 
               <DoubleAnimation Storyboard.TargetName="BackgroundRectangleSelected" Storyboard.TargetProperty="Opacity" Duration="0" To="1" /> 
              </Storyboard> 
             </vsm:VisualState> 
             <vsm:VisualState x:Name="MouseOver Selected"> 
              <Storyboard> 
               <DoubleAnimation Storyboard.TargetName="BackgroundRectangleSelected" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> 
              </Storyboard> 
             </vsm:VisualState> 
             <vsm:VisualState x:Name="Unfocused Selected"> 
              <Storyboard> 
               <DoubleAnimation Storyboard.TargetName="BackgroundRectangleSelected" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> 
               <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundRectangleSelected" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> 
                <SplineColorKeyFrame KeyTime="0" Value="Black"/> 
               </ColorAnimationUsingKeyFrames> 
              </Storyboard> 
             </vsm:VisualState> 
            </vsm:VisualStateGroup> 
           </vsm:VisualStateManager.VisualStateGroups> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="*"/> 
            <RowDefinition Height="Auto"/> 
            <RowDefinition Height="Auto"/> 
           </Grid.RowDefinitions> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="Auto" /> 
            <ColumnDefinition Width="*" /> 
           </Grid.ColumnDefinitions> 

           <Grid.Resources> 
            <Storyboard x:Key="DetailsVisibleTransition"> 
             <DoubleAnimation Storyboard.TargetName="DetailsPresenter" Storyboard.TargetProperty="ContentHeight" Duration="00:00:0.1" /> 
            </Storyboard> 
           </Grid.Resources> 

           <Rectangle x:Name="BackgroundRectangle" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="#FFBADDE9"/> 
           <Rectangle x:Name="BackgroundRectangleSelected" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="Black"/> 

           <localprimitives:DataGridRowHeader Grid.RowSpan="3" Name="RowHeader" localprimitives:DataGridFrozenGrid.IsFrozen="True" /> 
           <localprimitives:DataGridCellsPresenter Margin="2" Grid.Column="1" Name="CellsPresenter" localprimitives:DataGridFrozenGrid.IsFrozen="True" /> 
           <localprimitives:DataGridDetailsPresenter Grid.Row="1" Grid.Column="1" Name="DetailsPresenter" /> 
           <Rectangle Grid.Row="2" Grid.Column="1" Name="BottomGridLine" HorizontalAlignment="Stretch" Height="1" /> 
          </localprimitives:DataGridFrozenGrid> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </local:DataGrid.RowStyle> 
1

你可以在创建一个样式像这样的样本,以突出和调整我:

<!-- Right Aligned Cell --> 
    <Style x:Key="RightAlignedCell" TargetType="{x:Type dg:DataGridCell}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type dg:DataGridCell}"> 
        <Grid Background="{TemplateBinding Background}"> 
         <ContentPresenter HorizontalAlignment="Right" VerticalAlignment="Center"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="dg:DataGridCell.IsSelected" Value="True"> 
       <Setter Property="Background" Value="#356815" /> 
       <Setter Property="Foreground" Value="#e2fce2" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

    <!-- Center Aligned Cell --> 
    <Style x:Key="CenterAlignedCell" TargetType="{x:Type dg:DataGridCell}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type dg:DataGridCell}"> 
        <Grid Background="{TemplateBinding Background}"> 
         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="dg:DataGridCell.IsSelected" Value="True"> 
       <Setter Property="Background" Value="#356815" /> 
       <Setter Property="Foreground" Value="#e2fce2" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

后来你把:

<dg:DataGrid x:Name="dg" AutoGenerateColumns="False" 
      xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" ItemsSource="{Binding}" 
      SelectionMode="Single" ...> 
    <dg:DataGridTextColumn Header="Total Amount" Width="110" CellStyle="{StaticResource RightAlignedCell}" 
            Binding="{Binding Total,StringFormat={}\{0:N0\}}" IsReadOnly="True"/> 
    <dg:DataGridTextColumn Header="Enter Date" Width="110" CellStyle="{StaticResource CenterAlignedCell}" 
            Binding="{Binding EnterDate,StringFormat={}\{0:dd/MM/yyyy\}}" IsReadOnly="True"/> 

....

我希望这个作品的人谁的徘徊如何突出在WPF数据网格中的整行。

+0

你的建议的工作,但我有一个问题。我定义DataGridCell一个通用触发(例如设置不同的背景,如果它是只读)。然而,这个通用触发好像没有,如果我申请的RightAlignedCell样式而不触发工作。向你的风格添加触发器(就像你在这里所做的那样)工作正常。你能解释为什么吗? – newman 2011-05-15 17:34:44