由于我还没有找到任何答案,我相信我正在为如何提出这个问题而苦苦挣扎。我有一个WPF C#应用程序,其中包含一个父窗口和该父窗口内的各种UserControl。调整父窗口的大小时(显示虚拟键盘时),我需要将UserControl内部的控件居中于调整后的Window / Usercontrol中。例如,在MainADDmin窗口中,我有一个“ AddClientPersonnel” UserControl。当用户单击(将焦点移到)“名字”文本框时,将调整MainAddmin窗口的大小以允许使用虚拟键盘,而“ AddClientPersonnel” UserControl的窗口大小将不会调整(根据需要);但是,UserControl保持在同一位置,您看不到最初将焦点移到的文本框。
我该如何解决?此外,如果有很好的方法问这个问题,请告诉我。谢谢!
查看
<UserControl x:Class="OnPointEmp7a5MSSQL.Views.AddClientPersonnel"
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:local="clr-namespace:OnPointEmp7a5MSSQL.Views"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="AliceBlue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=".80*"/>
<RowDefinition Height=".20*"/>
</Grid.RowDefinitions>
<ScrollViewer x:Name="scrollviewer"
VerticalScrollBarVisibility="Auto"
PanningMode="VerticalFirst"
>
<Grid Grid.Row="0" MinHeight="900" Height="{Binding ActualHeight, ElementName=scrollviewer}">
<Grid.RowDefinitions>
<RowDefinition Height=".10*"/>
<RowDefinition Height=".10*"/>
<RowDefinition Height=".05*"/>
<RowDefinition Height=".10*"/>
<RowDefinition Height=".05*"/>
<RowDefinition Height=".10*"/>
<RowDefinition Height=".05*"/>
<RowDefinition Height=".10*"/>
<RowDefinition Height=".05*"/>
<RowDefinition Height=".10*"/>
<RowDefinition Height=".10*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".075*"/>
<ColumnDefinition Width=".25*"/>
<ColumnDefinition Width=".025*"/>
<ColumnDefinition Width=".025*"/>
<ColumnDefinition Width=".45*"/>
<ColumnDefinition Width=".175*"/>
</Grid.ColumnDefinitions>
<!--Specialty-->
<Viewbox Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right">
<Border Style="{StaticResource StandardLabelBorder}" HorizontalAlignment="Right">
<Label Content="Specialty"/>
</Border>
</Viewbox>
<Viewbox Grid.Row="3" Grid.Column="4" Stretch="Uniform">
<ComboBox Name="cboSpecialty" Style="{StaticResource StandardCombobox}"
DisplayMemberPath="Specialty"
SelectedValuePath="SpecialtyNum"
ItemsSource="{Binding SpecialtiesList}"
SelectedValue="{Binding SelectedSpecialtyNum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</Viewbox>
<!--First Name-->
<Viewbox Grid.Row="5" Grid.Column="1" HorizontalAlignment="Right">
<Border Style="{StaticResource StandardLabelBorder}" HorizontalAlignment="Right">
<Label Content="First Name"/>
</Border>
</Viewbox>
<Viewbox Grid.Row="5" Grid.Column="4" HorizontalAlignment="Left">
<TextBox Name="txtFirstName" Style="{StaticResource StandardTextBox}"
VerticalContentAlignment="Center"
Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
GotFocus="OpenKeyboard"
LostFocus="CloseKeyboard"
/>
</Viewbox>
<Viewbox Grid.Row="5" Grid.Column="5" HorizontalAlignment="Left">
<TextBlock Style="{StaticResource AsteriskTextBlock1}"/>
</Viewbox>
<!--Last Name-->
<Viewbox Grid.Row="7" Grid.Column="1" HorizontalAlignment="Right">
<Border Style="{StaticResource StandardLabelBorder}" HorizontalAlignment="Right">
<Label Content="Last Name"/>
</Border>
</Viewbox>
<Viewbox Grid.Row="7" Grid.Column="4" HorizontalAlignment="Left">
<TextBox Name="txtLastName" Style="{StaticResource StandardTextBox}"
VerticalContentAlignment="Center"
Text="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</Viewbox>
<Viewbox Grid.Row="7" Grid.Column="5" HorizontalAlignment="Left">
<TextBlock Style="{StaticResource AsteriskTextBlock1}"/>
</Viewbox>
<!--Street Address-->
<Viewbox Grid.Row="9" Grid.Column="1" HorizontalAlignment="Right">
<Border Style="{StaticResource StandardLabelBorder}" HorizontalAlignment="Right">
<Label Content="Street Address"/>
</Border>
</Viewbox>
<Viewbox Grid.Row="9" Grid.Column="4" HorizontalAlignment="Left">
<TextBox Name="txtStreetAddress" Style="{StaticResource StandardTextBox}"
VerticalContentAlignment="Center"
Text="{Binding StreetAddress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</Viewbox>
<!--City-->
<Viewbox Grid.Row="11" Grid.Column="1" HorizontalAlignment="Right">
<Border Style="{StaticResource StandardLabelBorder}" HorizontalAlignment="Right">
<Label Content="City"/>
</Border>
</Viewbox>
<Viewbox Grid.Row="11" Grid.Column="4" HorizontalAlignment="Left">
<TextBox Name="txtCity" Style="{StaticResource StandardTextBox}"
VerticalContentAlignment="Center"
Text="{Binding City, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</Viewbox>
<!--State-->
<Viewbox Grid.Row="13" Grid.Column="1" HorizontalAlignment="Right">
<Border Style="{StaticResource StandardLabelBorder}" HorizontalAlignment="Right">
<Label Content="State"/>
</Border>
</Viewbox>
<Viewbox Grid.Row="13" Grid.Column="4" HorizontalAlignment="Left">
<ComboBox Name="cboState" Style="{StaticResource StateComboBox}"
DisplayMemberPath="StateAbb"
SelectedValuePath="StateNum"
ItemsSource="{Binding StateAbbList}"
SelectedValue="{Binding SelectedStateNum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</Viewbox>
<!--Zip Code-->
<Viewbox Grid.Row="13" Grid.Column="1" HorizontalAlignment="Right">
<Border Style="{StaticResource StandardLabelBorder}" HorizontalAlignment="Right">
<Label Content="Zip Code"/>
</Border>
</Viewbox>
<Viewbox Grid.Row="13" Grid.Column="4" HorizontalAlignment="Left">
<TextBox Name="txtZipCode" Style="{StaticResource StandardTextBox}"
VerticalContentAlignment="Center"
Text="{Binding ZipCode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</Viewbox>
</ScrollViewer>
</Grid>
</Grid>
隐藏代码
private AddClientPersonnelVM _viewModel;
private VirtualKeyboard _virtualKeyboard;
public AddClientPersonnel()
{
InitializeComponent();
_viewModel = new AddClientPersonnelVM();
this.DataContext = _viewModel;
_virtualKeyboard = new VirtualKeyboard();
}
public void OpenKeyboard(object sender, RoutedEventArgs e)
{
_virtualKeyboard.OpenKeyboardResize(sender, e);
}
public void CloseKeyboard(object sender, RoutedEventArgs e)
{
_virtualKeyboard.CloseKeyboardResize(sender, e);
}
虚拟键盘类
public class VirtualKeyboard
{
public void OpenKeyboardResize(object sender, RoutedEventArgs e)
{
foreach (Window window in Application.Current.Windows)
{
if (window is WindowMainADDmin || window is WindowMainCaseBooking || window is WindowMainCaseManagement)
{
if (window.WindowState == WindowState.Maximized || window.WindowState == WindowState.Minimized)
window.WindowState = WindowState.Normal;
var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
window.Top = desktopWorkingArea.Top;
window.Left = desktopWorkingArea.Left;
window.Width = Math.Abs(desktopWorkingArea.Left - desktopWorkingArea.Right);
window.Height = Math.Abs(desktopWorkingArea.Top - desktopWorkingArea.Bottom) * 0.6;
}
}
}
public void CloseKeyboardResize(object sender, RoutedEventArgs e)
{
foreach (Window window in Application.Current.Windows)
{
if (window is WindowMainADDmin || window is WindowMainCaseBooking || window is WindowMainCaseManagement)
{
if (window.WindowState == WindowState.Maximized || window.WindowState == WindowState.Normal)
{
var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
window.Top = desktopWorkingArea.Top;
window.Left = desktopWorkingArea.Left;
window.Width = Math.Abs(desktopWorkingArea.Left - desktopWorkingArea.Right);
window.Height = Math.Abs(desktopWorkingArea.Top - desktopWorkingArea.Bottom) * 1;
}
}
}
}
}