BartekW
Topic Author
Posts: 53
Joined: 24 Mar 2021, 14:33

Can't focus User Control using Focus()

07 May 2021, 09:58

Hey, I have issue, I binded UserControl from C++ to ContentControl in WPF and after create new() UserControl I can't focus that using Focus(). Take a look
currentControl = new Tab2Content();	
bool focused = currentControl->Focus();
focused bool returns false and I don't know why.
Note The new view is generating

I have binded currentControl using NsProp in ContentControl here:
<Grid Background="Black" Height="450" VerticalAlignment="Top" Opacity="1">
<ContentControl x:Name="RootContent" Content="{Binding Content}"/>
                </Grid>
currentControl is UserControl* type
Tab2Content class is UserControl type too

Am I missing something somewhere?
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Can't focus User Control using Focus()

07 May 2021, 10:29

UserControl class overrides Focusable property and set its default value to "False", so if you need to Focus() your user control then you should set Focusable="True" first.
 
BartekW
Topic Author
Posts: 53
Joined: 24 Mar 2021, 14:33

Re: Can't focus User Control using Focus()

07 May 2021, 10:42

UserControl class overrides Focusable property and set its default value to "False", so if you need to Focus() your user control then you should set Focusable="True" first.
Yea, I did it before look:
<UserControl x:Class="MyControls.UserControls.Tab2Content"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
                xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
                xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
             mc:Ignorable="d" 
             xmlns:local="clr-namespace:MyControls.UserControls"
             IsFocusable="True">
And still can't focus :/
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Can't focus User Control using Focus()

07 May 2021, 10:56

The property is Focusable:
<UserControl x:Class="MyControls.UserControls.Tab2Content"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
                xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
                xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
             mc:Ignorable="d" 
             xmlns:local="clr-namespace:MyControls.UserControls"
             Focusable="True">
You should be getting a parsing error if using IsFocusable instead.
 
BartekW
Topic Author
Posts: 53
Joined: 24 Mar 2021, 14:33

Re: Can't focus User Control using Focus()

07 May 2021, 11:14

Actually I tryed ur version and its still the same. Should I execute this Focus() method in specified place? Becouse I call this in VM class, which dont inheritance from any UIElement. It inheritance from NoesisApp::NotifyPropertyChangedBase
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Can't focus User Control using Focus()

07 May 2021, 11:32

The next thing you have to take into account is that a control can only be focused if it is part of the UI tree, and it is visible and enabled. So if you are actually creating that Tab2Content as you mentioned in your initial code, you should wait until Tab2Content Loaded event occurs to focus it. You can do that with an EventTrigger in your user control xaml:
<UserControl ...>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
      <noesis:SetFocusAction/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
  ...
</UserControl>
 
BartekW
Topic Author
Posts: 53
Joined: 24 Mar 2021, 14:33

Re: Can't focus User Control using Focus()

07 May 2021, 11:48

Yea with empty action without TargetName it seems do nothing but I made that
<i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <noesis:SetFocusAction TargetName="SubTab1"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
I made that into Tab2Content.xaml
And It works almost like I want, but I think I would like to pass focus from outside becouse I want to pass focus after Enter on the tab.
So basically it looks like: We focus on tab it generates xaml, we enter that tab we go into that generate xaml with focus.
 
BartekW
Topic Author
Posts: 53
Joined: 24 Mar 2021, 14:33

Re: Can't focus User Control using Focus()

07 May 2021, 11:58

So, I checked
bool loaded = currentControl->IsLoaded();
and it returns false hmm
How Can I wait for it?
CoomandInvoke in Loaded trigger?
 
BartekW
Topic Author
Posts: 53
Joined: 24 Mar 2021, 14:33

Re: Can't focus User Control using Focus()

07 May 2021, 12:13

Okey, finally Thanks sfernandez. You gave me a thoughts how to make it and I make Focus() in OnLoaded method
currentControl = new Tab2Content();	
	
	currentControl->Loaded() += MakeDelegate(this, &paNVMTestScreen::OnLoaded);
Finally it pass Focus
1. but Idk how to set focus on the specified element in xaml after Focus(). Is here any trigger event something like "Get Focus"?

That doesnt work:
<i:Interaction.Triggers>
            <i:EventTrigger EventName="GotFocus">
                <noesis:SetFocusAction TargetName="SubTab1"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
2. And the second thing, can I have that
UserControl* currentControl
kept current? Something like binding but reverse direction from xaml to C++ I mean If I have binded that to Content in ContentControl if I make
<ei:ChangePropertyAction TargetName="RootContent" PropertyName="Content" Value="{StaticResource tab3Content}" />
Can I somehow receive it in code it changed?

Who is online

Users browsing this forum: Semrush [Bot] and 42 guests