【发布时间】:2014-11-01 15:45:36
【问题描述】:
我必须实现一个 WPF DataGrid,它可以只选择一行中的单元格。我怎么能做到这一点?我知道属性SelectionUnit 和SelectionMode,但是这两个属性的每个组合都没有成功。
XAML:
<DataGrid AutoGenerateColumns="True" CanUserAddRows="False"
ItemsSource="{Binding DPGridCR}" HeadersVisibility="None" SelectionUnit="Cell"
SelectionMode="Extended" CanUserResizeRows="False" />
此时我只能选择多行中的多个单元格,或者只选择一个单元格或选择一整行。但我想在一行中选择多个单元格。
编辑:
<UserControl x:Class="DesignerPro.Controls.DZLeerformularGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ViewModel="clr-namespace:ViewModel;assembly=ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="DZLeerformularGridControl">
<UserControl.Resources>
<ViewModel:DZLeerformularGridViewModel x:Key="ViewModel" />
</UserControl.Resources>
<DataGrid AutoGenerateColumns="True" CanUserAddRows="False"
ItemsSource="{Binding DPGridCR}" HeadersVisibility="None" SelectionUnit="Cell"
SelectionMode="Extended" CanUserResizeRows="False">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedCellsChanged">
<ei:CallMethodAction TargetObject="{Binding Mode=OneWay, Source={StaticResource ViewModel}}" MethodName="DataGrid_SelectedCellsChanged" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</UserControl>
使用EventTrigger,我尝试将SelectedCellsChanged-Event 绑定到我的ViewModel。您的解决方案在代码隐藏中运行良好,但在我的 ViewModel 中它不起作用:(
【问题讨论】:
-
你是用CTRL键做多选吗?
-
没有。我只是使用 WPF 默认 DataGrid 和几个
SelectionUnit和SelectionMode组合。 -
我的意思是,在您设置 SelectionUnit="Cell" 和 SelectionMode="Extended" 之后,它应该可以工作。但是,为了进行多选,您需要在单击单元格时按住 CTRL。 (或者,您可以单击并拖动,如 Excel)如果您希望它以不同的方式操作,那么您需要编写一些自定义代码。
-
您的解决方案无法以我喜欢的方式运行。我想防止用户选择多行中的单元格。用户只能选择一行中的单元格。例如:用户选择第 8 行中的第 3 到第 7 个单元格。这是允许的,但用户不应该能够选择第八行和第九行中的单元格!有没有聪明的方法来做到这一点?