NumericUpDown change its value on CellTemplate

1 Answer 77 Views
GridView NumericUpDown
alex
Top achievements
Rank 2
Bronze
Iron
Iron
alex asked on 27 Oct 2022, 06:53 PM

Hello,

I have a xaml with RadGridView which contains 4 columns. One of the columns has DataMemberBinding to a Double number/property and a cell template with a NumericUpDown control which is binded to the same Double property.

When I click the RadNumericUpDown arrow to change the value and the control remains Focusded and click immediatly the CellHeader for sorting, sometimes the value of some rows are changed to be the value of other rows. This is happens when I click the header many times with RowVirtualization=False.

If I click the arrow of the RadnumericUpDown and then I click another control to remove the focus and then I can click the header many times and the issue can't be reproduced. The update source trigger is PropertyChanged.

Sometimes during sorting the value is changed and then restored to the correct one.

Please I would like to know if you can fix it or give me some workaround. This is a very simple requirement (sorting). 

1 Answer, 1 is accepted

Sort by
1
Accepted
Martin Ivanov
Telerik team
answered on 28 Oct 2022, 09:28 AM

To avoid this behavior, you can move the focus from the NumericUpDown control when you start the sorting process. To do this, you can use the Sorting event of the GridView.

private void RadGridView_Sorting(object sender, Telerik.Windows.Controls.GridViewSortingEventArgs e)
{
	if (e.Column.UniqueName == "Number")
	{
		var focusedElement = (DependencyObject)FocusManager.GetFocusedElement(this);
		if (focusedElement is RadNumericUpDown || focusedElement.ParentOfType<RadNumericUpDown>() != null)
		{
			var cell = focusedElement.ParentOfType<GridViewCell>();
			if (cell != null)
			{
				cell.Focus();
			}
		}
	}
}

alex
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 02 Nov 2022, 06:00 PM

Thank you it works (altough I don't like the solution, ot should not happend).

I created an attached property because we work only MVVM without code behind.

In order to get the focused element in static context I find in the tree view the window and put it instead of "this" in your solution.

 

How can I find the focus element without looking for control of type Window ?

In the future I can put the grid in other controls

Martin Ivanov
Telerik team
commented on 03 Nov 2022, 09:46 AM

I am not sure that I understand your question, but you should be able to access the MainWindow using the static App.Current.MainWindow property. This way you will avoid the searching for a Window element. Alternatively, you should get the same effect by using gridview instance instead of "this". 

var focusedElement = (DependencyObject)FocusManager.GetFocusedElement((RadGridView)sender);

 

 
alex
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 03 Nov 2022, 08:16 PM

Using gridView instance returns null
Martin Ivanov
Telerik team
commented on 07 Nov 2022, 12:02 PM

Sorry, I made a mistake with the RadGridView suggestion. By default, the only focus scope is the MainWindow of the application. So, you can use the Application.Current.MainWindow as the argument for GetFocusedElement.

var focusedElement = (DependencyObject)FocusManager.GetFocusedElement(Application.Current.MainWindow);

alex
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 23 Nov 2022, 06:36 AM

Hi, still focused element in null using MainWindow. I will use my workaround. 

Thank you

Tags
GridView NumericUpDown
Asked by
alex
Top achievements
Rank 2
Bronze
Iron
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or