C# PropertyGrid设置时滚动位置不更改
我在Windows窗体应用程序中有一个C# PropertyGrid设置时滚动位置不更改,c#,.net,windows,winforms,propertygrid,C#,.net,Windows,Winforms,Propertygrid,我在Windows窗体应用程序中有一个PropertyGrid。每当其所选对象发生更改时,它会将其垂直滚动值重置为0。我需要它留在原来的地方。下面的代码似乎没有任何作用。我尝试了性能布局和一系列其他解决方案,但都没有成功。有什么想法吗 int pos = MyGrid.VerticalScroll.Value; MyGrid.SelectedObject = SomeDifferentObject; MyGrid.VerticalScroll.Value = pos; VerticalScro
PropertyGrid
。每当其所选对象
发生更改时,它会将其垂直滚动
值重置为0。我需要它留在原来的地方。下面的代码似乎没有任何作用。我尝试了性能布局
和一系列其他解决方案,但都没有成功。有什么想法吗
int pos = MyGrid.VerticalScroll.Value;
MyGrid.SelectedObject = SomeDifferentObject;
MyGrid.VerticalScroll.Value = pos;
VerticalScroll
属性与PropertyGrid类无关。它引用控件的内部属性。滚动事件从不上升。VScrollBar
是系统.Windows.Forms.PropertyGridInternal.PropertyGridView
控件的子控件,该控件是无法直接访问的类
不管怎样,您都可以获得它,将PropertyGrid强制转换为控件,或者忽略PropertyGrid的控件属性的可浏览(false)
属性,然后找到VScrollBar
子项,存储当前值,然后在更改SelectedObject
属性后再次设置它。
请注意,此新控件的属性数可能与前一个控件不同,滚动条将仅设置为以前的位置。
更准确地说,您可以在设置新的SelectedObject
之前和之后获得显示的属性数量,并执行相对滚动
在这里,我通过其
找到PropertyGridView
,它是AccessibleRole.Table
,但您也可以通过其文本找到它(“PropertyGridView”
):
var-vScroll=propertyGrid1.Controls.OfType()
.Where(ctl=>ctl.AccessibilityObject.Role==AccessibleRole.Table)
.First().Controls.OfType().First();
var vScrollValue=vScroll.Value;
propertyGrid1.SelectedObject=[某个其他对象];
vScroll.Value=vScrollValue;
如果需要(如上所述),请将滚动条重新定位到相同的相对位置,代码可能会变为:
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
var propGrid = propertyGrid1.Controls.OfType<Control>()
.Where(ctl => ctl.AccessibilityObject.Role == AccessibleRole.Table).First();
var totalProperties = (int)propGrid.GetType().GetField("totalProps", flags).GetValue(propGrid);
var vScroll = propertyGrid1.Controls.OfType<Control>()
.Where(ctl => ctl.AccessibilityObject.Role == AccessibleRole.Table)
.First().Controls.OfType<VScrollBar>().First();
var vRelativeScroll = vScroll.Value / (float)totalProperties;
propertyGrid1.SelectedObject = [Some Other Object];
totalProperties = (int)propGrid.GetType().GetField("totalProps", flags).GetValue(propGrid);
vScroll.Value = (int)(vRelativeScroll * totalProperties);
BindingFlags flags=BindingFlags.NonPublic | BindingFlags.Instance;
var propGrid=propertyGrid1.Controls.OfType()
.Where(ctl=>ctl.AccessibilityObject.Role==AccessibleRole.Table).First();
var totalProperties=(int)propGrid.GetType().GetField(“totalProps”,flags).GetValue(propGrid);
var vScroll=propertyGrid1.Controls.OfType()
.Where(ctl=>ctl.AccessibilityObject.Role==AccessibleRole.Table)
.First().Controls.OfType().First();
var vRelativeScroll=vScroll.Value/(float)totalProperties;
propertyGrid1.SelectedObject=[某个其他对象];
totalProperties=(int)propGrid.GetType().GetField(“totalProps”,标志).GetValue(propGrid);
vScroll.Value=(int)(vRelativeScroll*totalProperties);
您是否尝试订阅该活动,并在那里重新定位?@schulmaster是的。解释得很好,效果也很好。多谢!
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
var propGrid = propertyGrid1.Controls.OfType<Control>()
.Where(ctl => ctl.AccessibilityObject.Role == AccessibleRole.Table).First();
var totalProperties = (int)propGrid.GetType().GetField("totalProps", flags).GetValue(propGrid);
var vScroll = propertyGrid1.Controls.OfType<Control>()
.Where(ctl => ctl.AccessibilityObject.Role == AccessibleRole.Table)
.First().Controls.OfType<VScrollBar>().First();
var vRelativeScroll = vScroll.Value / (float)totalProperties;
propertyGrid1.SelectedObject = [Some Other Object];
totalProperties = (int)propGrid.GetType().GetField("totalProps", flags).GetValue(propGrid);
vScroll.Value = (int)(vRelativeScroll * totalProperties);