2012-02-28 4 views
10

ネット4 WPFのDataGrid C#MMVMDataGrid SelectionUnit =選択された行のすべてのサポートを無効にしますか?

データバインディングとcollectionviewは私が積極的に選択した項目がビューののCurrentItemプロパティ経由で何のviewmodelに知らせるの世話をするWPFのDataGrid SelectionUnitは、完全な行です。これは、選択モードがフルローに設定されている読み取り専用グリッドで効果的です。

編集可能なグリッドがあります。だから私はSelectionUnit = Cellを設定して、どのセルが入っているのかを簡単に見分けることができます。今や、突然グリッドには選択項目を追跡する機能がありません。セルモードに設定されている場合、SelectedItemを設定することもできません。今、ビューモデルは常にそれが最初の行にあると思っています。 SelectedCellsChangedをグリッドで処理して、どの行を表示しているのか把握することができます。グリッドのSelectedItemを設定することができなくなったため、ビューモデルを認識させる方法がありません。

セル選択モードでグリッドにSelectedItemがまだ存在しない理由を理解できません。

私のグリッドにハードコーディングして、ItemCheckをSelectedCellsChangedイベントからMoveCurrentToを呼び出すために、自分のコレクションビューにキャストしようとすると、ビューのCurrentItemをグリッドと同期させる本当の方法がありますか?

いずれか、またはグリッドスタイルを変更して、編集可能なグリッドがあるときに行のハイライト効果を削除または減らします。

+0

"ハードコード"が必要な場合は、インターフェイスを作成してください –

+0

はい、これは私が今行っていることです。 – happyfirst

+0

私は 'DataGrid'ソースを見て、' Cell'モードでは 'SelectedItem'と同期しません –

答えて

0

私も同様の問題がありますので、ここではStyle iが使用されています(Copyied from Net)。 したがって、WhistlerBlueテーマをhttp://datagridthemesfromsl.codeplex.com/からコピーし、次のように変更します。お役に立てれば。

私は同じ問題を検索し、あなたがしなければならない CellSelectionUnitセットでの行にアクセスするには、簡単な解決策

を発見された

<!--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

DataGridXX.SelectedCells[0].item 

ます場合にのみ動作します時間(拡張モードではない)で1つのセルのみを選択できます。

関連する問題