2012-02-28 88 views
10

.NET 4的WPF的DataGrid C#MMVMDataGrid SelectionUnit =單元格禁用對選定行的所有支持?

在DataGrid SelectionUnit是整行,WPF數據綁定和的CollectionView採取讓我知道在視圖模型是什麼通過視圖的CURRENTITEM財產的主動選擇的產品負責。這適用於只讀網格,選擇模式設置爲fullrow。

現在我有一個可編輯的網格。因此,我設置了SelectionUnit = Cell,以便更容易地發現所在的單元格。突然間,網格不再有任何能力來跟蹤選擇項目。設置爲單元格模式時,我甚至無法設置SelectedItem。所以現在viewmodel總是認爲它在第一行。我可以處理網格中的SelectedCellsChanged來確定我在哪個行上,我無法讓viewmodel知道,因爲網格的SelectedItem不能再被設置!

我不明白爲什麼網格在單元格選擇模式下仍不能有SelectedItem。

爲了將ItemSource強制轉換爲我的collectionview以從SelectedCellsChanged事件調用MoveCurrentTo,有沒有其他MVVM真正的方法來保持視圖的CurrentItem與網格同步?

要麼是這樣,要麼當我有一個可編輯的網格時,改變網格樣式以刪除或減少行高亮效果。

+0

如果你必須「硬編碼」,創建一個接口 – 2012-02-28 21:16:11

+0

是的,這就是我現在所做的。 – happyfirst 2012-02-29 00:41:38

+0

我剛纔看了一下'DataGrid'源文件和'Cell'模式,它們不會與'SelectedItem'同步 – 2012-02-29 17:19:15

回答

0

我也有類似的問題,所以這裏是我使用的風格(從網上覆制)。 因此,您從http://datagridthemesfromsl.codeplex.com/複製WhistlerBlue主題並進行以下修改。希望這可以幫助。

<!--Cell--> 
<Style x:Key='CellStyle' TargetType="{x:Type controls:DataGridCell}" > 
    <Setter Property="Foreground" Value="{StaticResource ThemeForegroundBrush}" /> 
    <Setter Property="Height" Value="Auto" /> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
    <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
    <Setter Property="Cursor" Value="Arrow" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <!--Padding hack--> 
    <Setter Property="Padding" Value="2 5 2 5" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type controls:DataGridCell}"> 
       <Grid x:Name="Root" Background="Transparent"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*" /> 
         <ColumnDefinition Width="Auto" /> 
        </Grid.ColumnDefinitions> 
        <Rectangle x:Name="FocusVisual" Margin="0,-2,0,0" 
           Stroke="White" Fill="White" 
           Opacity="0" IsHitTestVisible="false"/> 
        <ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
             VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" 
             ContentTemplate="{TemplateBinding ContentTemplate}" Cursor="{TemplateBinding Cursor}"/> 
        <Rectangle x:Name="RightGridLine" VerticalAlignment="Stretch" Width="1" Grid.Column="1" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

</Style> 

<!-- DataGridRow --> 
<Style x:Key='RowStyle' TargetType="{x:Type controls:DataGridRow}"> 
    <Setter Property="Background" Value="Transparent" /> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type controls:DataGridRow}"> 
       <Border x:Name="DGR_Border" 
       Background="{TemplateBinding Background}" 
       BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="{TemplateBinding BorderThickness}" 
       SnapsToDevicePixels="True"> 
        <primitives:SelectiveScrollingGrid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="Auto"/> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 

         <Grid.RowDefinitions> 
          <RowDefinition Height="*"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height='Auto' /> 
         </Grid.RowDefinitions> 
         <Rectangle x:Name="Selected" Margin="0" Grid.RowSpan="2" Grid.ColumnSpan="2" 
           Fill="{StaticResource BtnOverFill}" Stroke="{StaticResource selectedStroke}" 
           Opacity="0"/> 
         <Rectangle x:Name="SelectedHighlight" Margin="1" Grid.RowSpan="2" Grid.ColumnSpan="2" 
           Stroke="#A0FFFFFF" 
           Opacity="0"/> 
         <primitives:DataGridRowHeader Grid.RowSpan="2" 
                primitives:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"          
                Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DataGrid}}, 
          Path=HeadersVisibility, 
          Converter={x:Static controls:DataGrid.HeadersVisibilityConverter}, 
          ConverterParameter={x:Static controls:DataGridHeadersVisibility.Row}}"/> 
         <Rectangle x:Name="Over" Margin="0" Grid.RowSpan="2" Grid.ColumnSpan="2" 
           Fill="{StaticResource hoverGradient}" 
           Stroke="{StaticResource hoverStroke}" 
           Opacity="0"/> 
         <primitives:DataGridCellsPresenter Grid.Column="1" 
             ItemsPanel="{TemplateBinding ItemsPanel}" 
             SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         <primitives:DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" 
                  x:Name='DetailsPresenter' 
                  primitives:SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DataGrid}}, Path=AreRowDetailsFrozen, Converter={x:Static controls:DataGrid.RowDetailsScrollingConverter}, ConverterParameter={x:Static controls:SelectiveScrollingOrientation.Vertical}}"                               
                  Visibility="{TemplateBinding DetailsVisibility}"                
                  /> 
         <Rectangle Height="1" HorizontalAlignment="Stretch" 
            x:Name="BottomGridLine" 
            Fill="{StaticResource HorizontalVerticalGridLinesBrush}" 
            Grid.Column="1" Grid.Row="2" /> 
        </primitives:SelectiveScrollingGrid> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property='IsSelected' Value='True'> 
         <Trigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="Selected" Storyboard.TargetProperty="Opacity" To="0.84"/> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="SelectedHighlight" Storyboard.TargetProperty="Opacity" To="1"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </Trigger.EnterActions> 
         <Trigger.ExitActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="Selected" Storyboard.TargetProperty="Opacity" To="0"/> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="SelectedHighlight" Storyboard.TargetProperty="Opacity" To="0"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </Trigger.ExitActions> 
         <!--<Setter Property="DetailsVisibility" Value="Visible" />--> 
        </Trigger> 
        <MultiTrigger > 
         <MultiTrigger.Conditions> 
          <Condition Property="IsMouseOver" Value="True" /> 
          <Condition Property="IsSelected" Value="False" /> 
         </MultiTrigger.Conditions> 
         <MultiTrigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="Over" Storyboard.TargetProperty="Opacity" To="0.73"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </MultiTrigger.EnterActions> 
         <MultiTrigger.ExitActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="Over" Storyboard.TargetProperty="Opacity" To="0"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </MultiTrigger.ExitActions> 
        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelected" Value="True" /> 
          <Condition Property="IsFocused" Value="False" /> 
         </MultiTrigger.Conditions> 
         <MultiTrigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="Selected" Storyboard.TargetProperty="Opacity" To="0.84"/> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="SelectedHighlight" Storyboard.TargetProperty="Opacity" To="1"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </MultiTrigger.EnterActions> 
         <MultiTrigger.ExitActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="Selected" Storyboard.TargetProperty="Opacity" To="0"/> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="SelectedHighlight" Storyboard.TargetProperty="Opacity" To="0"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </MultiTrigger.ExitActions> 
        </MultiTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
5

我正在尋找同樣的問題,並發現了一個簡單的解決方案

要使用SelectionUnit設置訪問到該行Cell你要做的:

DataGridXX.SelectedCells[0].item 

它的工作原理只有當你可以只選擇一個單元格(不是擴展模式)。

相關問題