WPF - Set Focus when a button is clicked - No Code Behind

前端 未结 7 1264
小蘑菇
小蘑菇 2020-11-29 03:53

Is there a way to set Focus from one control to another using WPF Triggers?

Like the following example:



        
相关标签:
7条回答
  • 2020-11-29 04:21

    Is this what you want?

        <TextBox Name="txtName"></TextBox>
        <TextBox Grid.Row="1" Name="txtAddress"></TextBox>
        <Button Grid.Row="2" Content="Finish">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <EventSetter Event="Click" Handler="MoveFocusOnClick" />
                </Style>
            </Button.Style>
            <!--<Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                </EventTrigger>
            </Button.Triggers>-->
        </Button>
    

    c#:

        public void MoveFocusOnClick(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(txtName); // Or your own logic
        }
    
    0 讨论(0)
提交回复
热议问题