Can't focus User Control using Focus()
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
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:
currentControl is UserControl* type
Tab2Content class is UserControl type too
Am I missing something somewhere?
Code: Select all
currentControl = new Tab2Content();
bool focused = currentControl->Focus();
Note The new view is generating
I have binded currentControl using NsProp in ContentControl here:
Code: Select all
<Grid Background="Black" Height="450" VerticalAlignment="Top" Opacity="1">
<ContentControl x:Name="RootContent" Content="{Binding Content}"/>
</Grid>
Tab2Content class is UserControl type too
Am I missing something somewhere?
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Can't focus User Control using Focus()
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.
Re: Can't focus User Control using Focus()
Yea, I did it before look: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.
Code: Select all
<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">
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Can't focus User Control using Focus()
The property is Focusable:
You should be getting a parsing error if using IsFocusable instead.
Code: Select all
<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">
Re: Can't focus User Control using Focus()
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
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Can't focus User Control using Focus()
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:
Code: Select all
<UserControl ...>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<noesis:SetFocusAction/>
</i:EventTrigger>
</i:Interaction.Triggers>
...
</UserControl>
Re: Can't focus User Control using Focus()
Yea with empty action without TargetName it seems do nothing but I made that
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.
Code: Select all
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<noesis:SetFocusAction TargetName="SubTab1"/>
</i:EventTrigger>
</i:Interaction.Triggers>
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.
Re: Can't focus User Control using Focus()
So, I checked
and it returns false hmm
How Can I wait for it?
CoomandInvoke in Loaded trigger?
Code: Select all
bool loaded = currentControl->IsLoaded();
How Can I wait for it?
CoomandInvoke in Loaded trigger?
Re: Can't focus User Control using Focus()
Okey, finally Thanks sfernandez. You gave me a thoughts how to make it and I make Focus() in OnLoaded method
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:
2. And the second thing, can I have that
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
Can I somehow receive it in code it changed?
Code: Select all
currentControl = new Tab2Content();
currentControl->Loaded() += MakeDelegate(this, &paNVMTestScreen::OnLoaded);
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:
Code: Select all
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<noesis:SetFocusAction TargetName="SubTab1"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Code: Select all
UserControl* currentControl
Code: Select all
<ei:ChangePropertyAction TargetName="RootContent" PropertyName="Content" Value="{StaticResource tab3Content}" />
Who is online
Users browsing this forum: No registered users and 103 guests