WPF Validation inside UI Virtualized ListBox
I need to do some kind of data validation in a WPF application I am developing
As far as I know, when you add ValidatesOnDataErrors=True
to the Binding, everytime the Binding is resolved (it can be every time teh value changes, it can be when focus is lost), Binding engine checks the binding source, through IDataErrorInfo
, access the interface, finds out if there is some error or not.
It sounds good, with only one caveat. In order for data to be validated, it needs to be rendered开发者_JAVA技巧. If you happen to have an ObservableCollection
of that entity bound to a ListBox
with VirtualizingStackPanel.IsVirtualizing=True
, only rendered items are validated. Since most items are outside viewport, they are not rendered.
How can I force every item to be validated? Removing UI virtualization is not an option.
I have found one way to do so. It's ugly. But it works. Assuming the UI virtualized list is myList
for (Int32 i = 0; i < myList.Items.Count; i++)
{
myList.ScrollIntoView(myList.Items[i]);
}
UpdateLayout();
That way, I get a list of all items. I scroll the list to every item in that list. And I update layout, because ScrollIntoView is asynchronous, I think. After that, every item has been rendered, every binding has been executed and every validation has been evaluated.
Are there better ways out there?
精彩评论