如何使用Modern UI将焦点设置在子元素上

本文关键字:元素 设置 焦点 何使用 Modern UI | 更新日期: 2023-09-27 18:16:08

我得到了一个LoginPage。xaml:

<mui:ModernWindow x:Class="NeoClinic.MAS.LoginPage"
                  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:mui="http://firstfloorsoftware.com/ModernUI"
                  Title="Login" IsTitleVisible="True"                         
                  ContentSource="Pages'LoginBasic.xaml"
                  WindowState="Maximized"
                  WindowStartupLocation="CenterScreen"
                  Closing="LoginPage_OnClosing"
                  >
    <mui:ModernWindow.MenuLinkGroups>
        <mui:LinkGroup x:Name="LoginLinkGroup" DisplayName="Login" >
            <mui:LinkGroup.Links>
                <mui:Link x:Name="LoginLink" DisplayName="Login" Source="Pages'LoginBasic.xaml" />
            </mui:LinkGroup.Links>
        </mui:LinkGroup>
    </mui:ModernWindow.MenuLinkGroups>
</mui:ModernWindow>

和UserControl:

<UserControl x:Class="NeoClinic.MAS.Pages.LoginBasic"
                 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:mui="http://firstfloorsoftware.com/ModernUI"
                 x:Name="BasicLogin"
                 mc:Ignorable="d" 
                 Height="550"
                 Width="1280">
        <Grid Style="{StaticResource ContentRoot}" >
            <Grid ShowGridLines="False" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <Grid x:Name="GridBanner" ShowGridLines="False" Grid.RowSpan="2" Grid.ColumnSpan="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.5*"/>
                        <ColumnDefinition/>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <Button x:Name="ImageButton" Content="Image"  Grid.Column="0" Width="150" HorizontalAlignment="Left"/>
                    <Label Content="MIDAS SYSTEM" FontStretch="ExtraExpanded" FontSize="70" 
                        HorizontalAlignment="Left" 
                        VerticalAlignment="Center" Grid.Column="1"  Margin="10" 
                        Grid.ColumnSpan="2"/>
                </Grid>
                <Border x:Name="GroupBoxLogin"  Grid.Column="1" Grid.Row="3" Grid.RowSpan="3" BorderBrush="White" BorderThickness="3" CornerRadius="10">
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="2*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Label Content="User Name" FontSize="20" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="5"/>
                        <Label Content="Password" FontSize="20" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Margin="5"/>
                        <Label x:Name="NotificationLabel" Content="" HorizontalAlignment="Right" FontSize="23" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Margin="5"/>
                        <TextBox x:Name="UserNameTextBox" Grid.Column="1" Grid.Row="0" FontWeight="Normal" FontSize="23" Margin="5,5,5,0"  PreviewKeyUp="UserNameTextBox_OnPreviewKeyUp"/>
                        <TextBox x:Name="PasswordTextBox" Grid.Column="1" Grid.Row="1"  FontWeight="Normal" FontSize="23" Margin="5,5,5,0" PreviewKeyUp="PasswordTextBox_OnPreviewKeyUp"/>

                        <Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3" Background="{x:Null}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <mui:ModernButton x:Name="OkButton" Content="Ok" Grid.Column="4"  Grid.ColumnSpan="2"  FontSize="20" FontWeight="Bold" Foreground="Black" Click="OkButton_OnClick"/>
                            <mui:ModernButton x:Name="ExitButton" Content="Exit" Grid.Column="6" Grid.ColumnSpan="2" FontSize="20" FontWeight="Bold" Foreground="Black" Click="ExitButton_OnClick"/>
                        </Grid>

                    </Grid>
                </Border>

            </Grid>
        </Grid>
    </UserControl>

我尝试了我在谷歌上研究的关于自动聚焦控件的一切,如文本框,但插入点只出现而不闪烁,当我按下一个键时,它不会出现在文本框中,除非我单击文本框并再次开始按键

这是我的登录页面的构造器代码:

public LoginPage()
{
    InitializeComponent();
}
这里是我在UserControl中的构造函数:
    public LoginBasic()
{
    InitializeComponent();       
    FocusManager.SetFocusedElement(this, UserNameTextBox);// the insertion appears but not active it seems
}

如何使用Modern UI将焦点设置在子元素上

我找到了

public LoginBasic()
        {
            InitializeComponent();
            //FocusManager.SetFocusedElement(this, UserNameTextBox);
            //Keyboard.Focus(UserNameTextBox);
            this.IsVisibleChanged += LoginControl_IsVisibleChanged; 

        }


void LoginControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if ((bool)e.NewValue == true)
        {
            Dispatcher.BeginInvoke(
            DispatcherPriority.ContextIdle,
            new Action(() => UserNameTextBox.Focus()));
        }
    } 

源是基于

相关文章: