.Net 4 WPF DataGrid C#MMVM
当DataGrid SelectionUnit为完整行时,wpf数据绑定和collectionview会通过视图的currentitem属性让我知道viewmodel中的主动选择项.这适用于只读网格,选择模式设置为fullrow.
现在我有一个可编辑的网格.因此,我设置了SelectionUnit = Cell,以便更容易找到一个单元格.现在,网格突然不再具有跟踪选择项目的能力.设置为单元格模式时,我甚至无法设置SelectedItem.所以现在viewmodel总是认为它在第一行.我可以在网格中处理SelectedCellsChanged以找出我所在的行,我无法让视图模型知道,因为网格的SelectedItem无法再设置!
我不明白为什么在单元格选择模式下网格仍然没有SelectedItem.
没有硬编码到我的网格中以将ItemSource转换为我的集合视图以从SelectedCellsChanged事件调用MoveCurrentTo,是否有任何其他MVVM真正的方法来保持视图的CurrentItem与网格同步?
或者,或者当我有一个可编辑的网格时,我更改网格样式以删除或减少行高亮效果.
小智 6
我正在寻找同样的问题,并找到了一个简单的解决方案
要访问具有SelectionUnitset 的行,Cell您必须执行以下操作:
DataGridXX.SelectedCells[0].item
Run Code Online (Sandbox Code Playgroud)
仅当您一次只能选择一个单元格(而不是在扩展模式下)时,它才有效.
小智 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>
Run Code Online (Sandbox Code Playgroud)