Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I have defined the style for a Popup control in my resource file and Popup itself in a UserControl.

Now i want that when my pop is Loaded the focus should be on the Button contained in the Popup Content of Popup

I want to do it in XAML only.

Below is my code:
HTML
<Style x:Key="myContentStyle" TargetType="{x:Type ContentControl}">
         <Setter Property="Template">
             <Setter.Value>
                 <ControlTemplate>
 
                <Button x:Name="DoneButton"
                                     HorizontalAlignment="Right"
                                     Grid.Row="4"
                                     Width="Auto"
                                     Margin="4,4,4,4"/>
 
        <ControlTemplate.Triggers>
                         <DataTrigger Binding="{Binding Path=IsOpen,RelativeSource={RelativeSource AncestorType=Popup}}" Value="True">
                            <Setter TargetName="DoneButton" Property="FocusManager.FocusedElement" Value="{Binding ElementName=DoneButton}" />
                         </DataTrigger>
                 </ControlTemplate.Triggers>
 
                </ControlTemplate>
             </Setter.Value>
         </Setter>
     </Style>
 




<!-- Embedded popup -->
 <Popup Style="{DynamicResourcemyPopupStyle}" > 
<ContentControlStyle="{DynamicResourcemyContentStyle}"/>
 
</Popup>

when i press the Tab button Focus is still on the Background windows controls.

Any help !!
Posted
Updated 13-Mar-12 16:10pm
v2

I am sorry this is the right solution

first create a style for the border:
<style x:key="borderStyle" targettype="{x:Type Border}" xmlns:x="#unknown">
    <eventsetter event="Loaded" handler="OnBorderLoaded" />
</style>


then create your logic in the code behind:
private void OnBorderLoaded(object sender, RoutedEventArgs e)
{
    var border = sender as Border;
    if (border != null)
    {
        border.Child.Focus();
    }
}


the create your popup style and make the border take the earlier style:
<style x:key="myPopupStyle" targettype="{x:Type Popup}" xmlns:x="#unknown">
    <setter property="Child">
        <setter.value>
            <border background="White" style="{StaticResource borderStyle}">
                <button name="DoneButton" content="Done Button">
                             HorizontalAlignment="Right"
                             Grid.Row="4"
                             Width="Auto"
                             Margin="4,4,4,4">
                </button>
            </border>
        </setter.value>
    </setter>
</style>


finally make your popup and give it the right style:
<popup style="{StaticResource myPopupStyle}" grid.row="1" />
 
Share this answer
 
As far as I know there is no way to do this with available controls.

If you are willing to build a simple control, I would inherit form PopUp, and catch the OnOpen event and set the focus. The control to get focus would be set by a dependency property.

Another option is to create a bahaviour which basically does the same thing. In this case the bahavior property to do this would be the control to get focus.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900