2009-06-26 79 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