C# 以编程方式取消选择DataGrid中的行
我正在努力找到解决办法 我试图做的是在DataGrid中只选择某些行。SelectionMode为FullRow。例如,如果用户尝试拖动选择几行,其中一行我不想选择。在本例中,我希望仍然选择有效行,而不是无效行C# 以编程方式取消选择DataGrid中的行,c#,wpf,datagrid,selection,wpfdatagrid,C#,Wpf,Datagrid,Selection,Wpfdatagrid,我正在努力找到解决办法 我试图做的是在DataGrid中只选择某些行。SelectionMode为FullRow。例如,如果用户尝试拖动选择几行,其中一行我不想选择。在本例中,我希望仍然选择有效行,而不是无效行 有什么想法吗?这不是最好的方法,但您可以创建一个内置类来保存网格的选定索引,然后如果选择了无效行,您只需将选定索引更改为最后一个有效索引即可。我相信这个解决方案也可以适应DataGrid 编辑 要测试它,请执行以下操作: public class ViewModel : INotifyP
有什么想法吗?这不是最好的方法,但您可以创建一个内置类来保存网格的选定索引,然后如果选择了无效行,您只需将选定索引更改为最后一个有效索引即可。我相信这个解决方案也可以适应DataGrid 编辑 要测试它,请执行以下操作:
public class ViewModel : INotifyPropertyChanged
{
#region INotifyPropertyChanged values
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
public List<Dummy> Elements { get; set; }
public ViewModel()
{
this.Elements = new List<Dummy>(){
new Dummy() { CanSelect =true, MyProperty = "Element1"},
new Dummy() { CanSelect =false, MyProperty = "Element2"},
new Dummy() { CanSelect =true, MyProperty = "Element3"},
new Dummy() { CanSelect =false, MyProperty = "Element4"},
new Dummy() { CanSelect =true, MyProperty = "Element5"},
new Dummy() { CanSelect =true, MyProperty = "Element6"},
new Dummy() { CanSelect =true, MyProperty = "Element7"},
new Dummy() { CanSelect =true, MyProperty = "Element8"},
new Dummy() { CanSelect =false, MyProperty = "Element9"},
};
}
}
public class Dummy
{
public bool CanSelect { get; set; }
public string MyProperty { get; set; }
public override string ToString()
{
return this.MyProperty;
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Window.Resources>
<Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="local:DataGridRowEx.CanSelect" Value="{Binding CanSelect}" />
</Style>
</Window.Resources>
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid x:Name="LayoutRoot">
<DataGrid ItemsSource="{Binding Elements}"
RowStyle="{DynamicResource DataGridRowStyle}"
SelectionUnit="FullRow" />
</Grid>
</Window>
它与multiselection一起工作,即使在按下shift键时也是如此。与ListBoxItem解决方案的唯一显著区别是取消选择必须使用Dispatcher.BeginInvoke排队,但不确定原因。
这种方法唯一需要注意的是,如果DataGrid有单一选择,则尝试选择一个不可选择的项目将取消选择当前选定的项目;如果DataGrid有扩展选择,则取消选择所有项目。这不是一个很好的解决方案,但您可以在鼠标启动事件中取消选择行,因此,让用户以编程方式选择全部,然后取消选择,我还没有测试过这一点
private void dgvReport_MouseUp(object sender, MouseEventArgs e)
{
foreach (DataGridViewRow row in this.dgvReport.SelectedRows) {
if (row.Cells[1].Value == "Invalid"){
this.dgvReport.Rows[row.Index].Selected = false;
}
}
}
这就行了,
int row = grdexam.SelectedIndex;
DataGridRow rv =(DataGridRow)this.grdexam.ItemContainerGenerator.ContainerFromIndex(row);
DataRowView rvv =(DataRowView)rv.Item;
MessageBox.Show(rvv.Row[1].ToString());
如何定义无效行?特定类型的项目将无效,因此类型检查将确定是否允许选择该行。不幸的是,它需要能够处理多个选择。相同的事情,但用于所选行?这是dataGridView对象,对吗?yourgrid.SelectedRows编辑:刚刚意识到dataGrid是一个独立的东西,所以没有Rows属性。编辑:或SelectedRows。@JamieKelly我假设您使用的是DataGridView?嗯,我已经用我的代码尝试过了,但没有用。但是在测试项目中,它可以工作,所以谢谢。我将再次尝试将其与我的代码集成。我需要为我的特定应用程序制定逻辑,但这有所帮助。item.Dispatcher.BeginInvokeAction=>item.IsSelected=false;我需要的是台词,谢谢。
int row = grdexam.SelectedIndex;
DataGridRow rv =(DataGridRow)this.grdexam.ItemContainerGenerator.ContainerFromIndex(row);
DataRowView rvv =(DataRowView)rv.Item;
MessageBox.Show(rvv.Row[1].ToString());