Error template get lost when changing tab in MahApps.Metro - mahapps.metro

In my window I have 2 tabs. One with the main informarion and the other one with an editable grid. When I click the save button the entire window gets validated and the controls with validation problems are highlighted in red. But if i change the tab to the one with the grid and get back to the one with the validation problems the controls are not highlighted anymore. Even if i click the save button again, and the validation happens, the controls never get highlighted again.
Here are the screen captures

it's a bit late, but today I had the same situation and found the solution.
You have to add a AdornerDecorator inside the TabItem:
<TabItem Header="Foo">
<AdornerDecorator>
//more content
</AdornerDecorator>
</TabItem>
I have found it in a more general question:
TextBox with validation loses ErrorTemplate on tab change
Greetings

Related

WPF ComboBox - If expanded, stays on top of all windows

I have a WPF form with ComboBox on it. If I expand my combobox, then open an app such as Notepad or any other, the app will cover my WPF form but the expanded ComboBox portion will still be on top. So it appears as the Notepad app is between the WPF form and its expanded portion of ComboBox (the combo box dropdown). It looks like the ComboBox will show its expanded dropdown always at the top of z-order.
This is causing few other issues such as beeing unable to close the WPF form.
This is simply wrong.
Above:
z-1 is z-order of my WPF form with expanded ComboBox (but note that the expanded area is on top of applications opened and overlapping the WPF form
z-2 is order of Notepad application opened and overlapping my WPF form with the ComboBox on it (again, you can see that Notepad overlaps WPF form but the ComboBox expanded dropdown overlaps Notepad. It should be behind Notepad, not in front of it)
z-3 is the expanded dropdown of my ComboBox located on WPF form with z-1 order
My ComboBox is part of UserControl and its xaml is like this
<ComboBox Name="MyComboBox"
ItemsSource="{Binding ItemsSet}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedItem="{Binding Path=SelectedItem}"
BorderBrush="Green"
BorderThickness="2">
</ComboBox>
How do I prevent the issue in the provides screenshot?
I understand that the behavior is in most cases "wrong"; when would you really want a dropdown to stay open if focus is lost on the current application? However, as I mentioned in my comment, that is the designed behavior of the Popup control. A WPF combobox under the hood is actually a ToggleButton with a Popup control that is set to open when the ToggleButton is toggled. So... the way to prevent the issue you're seeing is to prevent the Popup control from staying on top of all applications.
There's a StackOverflow question for that :)
Popup always stays on top
One of the answers actually has code snippet with a derived/custom popup control that uses user32.dll to prevent the undesired behavior.
So ok great, that's how you fix a Popup, but how do you fix a ComboBox? You simply have to override the default ControlTemplate to use the fixed-non-top-most Popup control instead of the standard WPF Popup.
I "fixed" this by setting the WPF form containing my ComboBox as TopMost="True" in its xaml. Not perfect fix but at least removes the stupid look and all other problems it leads to I discovered. I cannot believe that MS made this default behavior, it is plain stupid.

Silverlight control within DataGrid is reloaded when focus lost

I have a DataGrid in a Silverlight control which contains custom controls. These custom controls are used outside the grid too and work perfectly. When used within the grid however, they appear to reset themselves when focus is lost (i.e. we move to another cell).
To test that it wasn't my custom control, I added a column containing an AutoComplete box. When typing into the box and tabbing to the next column the box is cleared.
When debugging I can see that the controls loaded event is called after we lost focus from it, which confirms that it's being reloaded/recreated when moving to the next field!? Is it simply not committing the change to the grid for some reason?
e.g. of XAML (very basic, nothing weird here)
<sdk:DataGridTemplateColumn Header="Description">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<sdk:AutoCompleteBox Name="Description" Width="100" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
This is perfectly normal - the CellTemplate is used when the cell is rendered, and they get destroyed when the row is scrolled out of view if you have virtualisation turned on.
The CellEditingTemplate is used when the grid cell goes into editing mode, at this point your editing control will get created - IOW the editing control is created on demand. Just focussing on the cell is not sufficient to put the cell into edit mode by itself.

Tooltip Display Issue

I have added a tooltip into my code ToolTipService.ToolTip="Submit Changes" in Button control.
I also used a Radpane telerik control on my page.when i move mouse on button the tooltip is hiding behind Radpane control.
I want to display it on that telerik control. Please give me suggestion.
Thank you
If this still giving you issues try changing the z-order of the ToolTip in relation to the button so that it displays on top of the control. Hope this helps!
You must first set property to enable focus behaiviars.
set IsHitTestVisible="True"

wpf validation refresh between tabbed pages

I'm getting there with this WPF validation, IDataErrorInfo, INotifyPropertyChange stuff, but new issue on refresh. I have a window with a bunch of controls on it. I have a tabbed page interface and for simplicity have 2 pages... 5 controls on each.
I start to "Add" a record, so all fields are blank. Ex: 3 of the fields on the first page that require validation are properly flagged as red border to indicate they are required... no problem.
Now, I click on to page 2 and immediately back to page 1. The red borders are all gone. They don't reappear red unless I explicitly tab through them to re-focus them, lose focus and force it to do its lostfocus / property changed validation via IDataErrorInfo.
First, any explanation WHY WPF is losing what the first page looks like just because it has to change page 1's visiblity to show page 2, and then back to page 1.
Second, suggestions on how to force the controls to be properly refreshed with the red border indicating they are required.
Thanks
Just put the controls you validate inside an AdornerDecorator an it will work fine ;)
<TabItem>
<AdornerDecorator>
<Grid>
<TextBox>
</TextBox>
...
</Grid>
</AdornerDecorator>
</TabItem>
If I recall correctly, the default error validation markers for controls are just red boxes overlaid over the top of the control, not part of the control itself, so the visibility of that overlay does not persist if the control is hidden. You might try changing the control template to incorporate a red border / label directly as part of the control, and that issue might be cleared up.
To force all your bindings to refresh (and therefore their validation) all you need to do is call RaisePropertyChanged again for any property you want to re-validate. It's a little wasteful, but you can just call RaisePropertyChanged with an empty string as parameter, and it will raise for all properties in the viewmodel.

WPF FocusManager interferring with focus

I have a grid, and in the grid I am setting my first element to be focused:
<Grid FocusManager.FocusedElement="{Binding ElementName=companyNameField}">
When the window opens, the correct control is focused.
But if I tab through the whole form, when the above focused field, should have the focus, there is no cursor evident anywhere on the window.
If I hit tab once more, it selects the control after the control that should be selected.
If I completely remove the focumanager attributes from my grid, I am correctly tabbing trough all my controls in the correct order.
Even stranger, if i leave in the focusmanager attributes and first click on the first text box and then focus through the entire form, then it selects my text box like any other control.
If you have any Ideas, I would love some help.
Thanks
I have had this happen to me, when I had some focus code in the code-behind fighting with the FocusManager. Mine was hidden in the Load of a nested UserControl, so I didn't notice it.

Resources