
public static voID SelectColumn(DataGrID grID,int column){ for (int i = 0; i < grID.Items.Count; i++) { // Select each cell in this column var cell = DataGrIDHelper.GetCell(grID,i,column); if (cell != null) { cell.IsSelected = true; } } DataGrIDHelper.GetCell(grID,column).Focus();}public static DataGrIDCell GetCell(DataGrID grID,int row,int column){ DataGrIDRow rowContainer = GetRow(grID,row); if (rowContainer != null) { DataGrIDCellsPresenter presenter = TreeHelper.GetVisualChild<DataGrIDCellsPresenter>(rowContainer); if (presenter == null) { // may be virtualized,bring into vIEw and try again grID.ScrollintoVIEw(rowContainer,grID.Columns[column]); presenter = TreeHelper.GetVisualChild<DataGrIDCellsPresenter>(rowContainer); } if (presenter != null) { // try to get the cell but it may possibly be virtualized DataGrIDCell cell = (DataGrIDCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); if (cell == null) { // may be virtualized,bring into vIEw and try again grID.ScrollintoVIEw(rowContainer,grID.Columns[column]); cell = (DataGrIDCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); } return cell; } } return null;}public static DataGrIDRow GetRow(DataGrID grID,int index){ DataGrIDRow row = (DataGrIDRow)grID.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { // may be virtualized,bring into vIEw and try again grID.ScrollintoVIEw(grID.Items[index]); row = (DataGrIDRow)grID.ItemContainerGenerator.ContainerFromIndex(index); } return row;} 更新:
我正在尝试@ianschol建议的解决方案.这就是我所拥有的(我在b / c后面的代码中绑定我不知道在运行时需要多少列):
for (int i = 0; i < this.CurrentData.Data[0].Length; i++) { TheGrID.Columns.Add( new DataGrIDTextColumn { header = (this.CurrentData.Rank > 1) ? string.Format(this.culture,headerFormatString,i + 1) : string.Empty,Binding = new Binding(string.Format("[{0}].DataValue",i)) { ValIDatesOnDataErrors = true,UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged },WIDth = DataGrIDLength.auto,ElementStyle = new Style { targettype = typeof(TextBlock),Triggers = { this.errorTrigger } },EditingElementStyle = new Style { targettype = typeof(TextBox),CellStyle = new Style { targettype = typeof(DataGrIDCell),Setters = { new Setter { Property = DataGrIDCell.IsSelectedProperty,Value = new Binding(string.Format("[{0}].IsSelected",i)) { Mode = BindingMode.TwoWay,} },} }); } 和我的IsSelected财产:
private bool isSelected = false; public bool IsSelected { get { return this.isSelected; } set { this.isSelected = value; OnPropertyChanged("IsSelected"); } } 而新的SelectColumn代码:
public static voID SelectColumn(DataGrID grID,int column) { for (int i = 0; i < grID.Items.Count; i++) { // Select each cell in this column ((DataItem[])(grID.Items[i]))[column].IsSelected = true; } } 问题是,如果我在代码中更新IsSelected属性,它会更新GUI(有点,它古怪),但反之亦然.即如果我在GUI中选择一个单元格/行,它不会在代码中调用属性设置器.你可以看到绑定是TwoWay所以我不确定这个问题.
另一个更新:问题似乎与虚拟化有关.如果我关闭虚拟化(VirtualizingStackPanel.IsVirtualizing =“False”),它可以正常工作.
解决方法 一种更有效的方法可能是在DataSource的类上使用IsSelected属性,这样每列都有一个相应的“IsSelected”属性.public class MyData : INotifyPropertyChanged{ private string name; public string name { get { return name; } set { name = value; Notify("name"); } } private bool nameSelected = false; public bool nameSelected { get { return nameSelected; } set { nameSelected = value; Notify("nameSelected"); } } //... etc ...} 接下来,您可以更改每个Column的CellStyle,以将单元格的IsSelected属性绑定到类上的相关IsSelected属性.
<DataGrID ItemsSource="{Binding Users}" autoGenerateColumns="False" HorizontalAlignment="left" name="scratchGrID" CanUserAddRows="False" VerticalScrollbarVisibility="auto" SelectionUnit="Cell"> <DataGrID.Columns> <DataGrIDTextColumn Binding="{Binding name}" header="User name" WIDth="200"> <DataGrIDTextColumn.CellStyle> <Style targettype="{x:Type DataGrIDCell}"> <Setter Property="IsSelected" Value="{Binding nameSelected}" /> </Style> </DataGrIDTextColumn.CellStyle> </DataGrIDTextColumn> <DataGrIDTextColumn Binding="{Binding Age}" header="User Age" WIDth="80"> <DataGrIDTextColumn.CellStyle> <Style targettype="{x:Type DataGrIDCell}"> <Setter Property="IsSelected" Value="{Binding AgeSelected}" /> </Style> </DataGrIDTextColumn.CellStyle> </DataGrIDTextColumn> </DataGrID.Columns> </DataGrID> 最后,像这样实现你的select-all代码(这会在Age上选择all,你可能希望做一个更通用/更优雅的实现;)):
foreach (MyData user in Users) { user.AgeSelected = true; } 您必须注意确保所有NotifyPropertyChanged行为都排成一行,因为您希望网格能够识别其绑定集合中的属性正在更新.
总结以上是内存溢出为你收集整理的c# – DataGrid选择列全部内容,希望文章能够帮你解决c# – DataGrid选择列所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)