- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我浏览过其他一些有关WPF和关注点的文章,据我所知,我的代码中确实缺少一些东西。我正在使用严格的MVVM开发应用程序,因此我试图避免 View 文件中出现任何代码隐藏(在需要代码隐藏的情况下使用附加行为),但此时甚至将焦点代码放在代码中- View 的后面是行不通的。
我有一个带有主窗口的应用程序,并且试图在热键上弹出搜索窗口。我想在用户按下热键后立即将键盘焦点放在搜索文本上,因此它只是热键,然后键入您的搜索词。尽管逻辑焦点在此点上仍在起作用,即使键盘声称已将焦点放在元素上。
我似乎无法从代码中同时获取键盘和逻辑焦点。但是,如果我在搜索框出现后立即点击Tab键,那么我将直接进入文本框。
主窗口代码:
<ribbon:RibbonWindow x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
xmlns:attached="clr-namespace:UserInterface.Attached"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
xmlns:viewModels="clr-namespace:UserInterface.ViewModels"
xmlns:views="clr-namespace:UserInterface.Views"
xmlns:layout="clr-namespace:UserInterface.ViewModels.Layout"
xmlns:layout1="clr-namespace:UserInterface.Views.Layout"
MinHeight="560"
MinWidth="950"
WindowStartupLocation="CenterScreen"
Icon="{Binding Layout.IconPath}"
DataContext="{Binding Main, Source={StaticResource Locator}}"
FocusManager.FocusedElement="{Binding ElementName=LayoutControl}"
Title="{Binding Layout.Title}">
<!-- Ribbon menu shortcuts -->
<Window.InputBindings>
<KeyBinding Modifiers="Control" Key="T" Command="{Binding Layout.Commands[GlobalObjectSearch]}" />
</Window.InputBindings>
<Grid>
<ContentPresenter Content="{Binding Layout}" x:Name="LayoutControl">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type layout:MainViewModel}">
<layout1:MainView/>
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
</ribbon:RibbonWindow>
public SelfClosingDialogView ShowSelfClosingDialog(IWindowDialogViewModel dataContext)
{
dataContext.CheckWhetherArgumentIsNull(nameof(dataContext));
var view = new SelfClosingDialogView
{
DataContext = dataContext,
Owner = Application.Current?.MainWindow
};
view.Show();
return view;
}
<Window x:Class="UserInterface.Views.DialogViews.SelfClosingDialogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:attached="clr-namespace:UserInterface.Attached"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
SizeToContent="WidthAndHeight"
WindowStyle="None"
WindowStartupLocation="CenterOwner">
<!-- Allow view models to cause the window to close -->
<Window.Style>
<Style TargetType="{x:Type Window}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsClosed}" Value="true">
<!-- Executes close -->
<Setter Property="attached:WindowCloseBehavior.Close" Value="true" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
<!-- Displays the passed-in view model -->
<Grid>
<ContentPresenter x:Name="DialogPresenter" Content="{Binding}" Margin="0" />
</Grid>
</Window>
<UserControl x:Class="UserInterface.Views.DialogViews.ObjectSearchView"
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:dialogViewModels="clr-namespace:UserInterface.ViewModels.DialogViewModels"
xmlns:utils="clr-namespace:WPF.Utils"
xmlns:attached="clr-namespace:UserInterface.Attached"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance dialogViewModels:ObjectSearchViewModel}"
MinWidth="250"
Focusable="True"
FocusManager.IsFocusScope="True">
<UserControl.InputBindings>
<KeyBinding Key="Enter" Command="{Binding BrowseToObjectCommand}" />
<KeyBinding Key="Escape" Command="{Binding CloseWindowCommand}" />
</UserControl.InputBindings>
<UserControl.Resources>
<Style BasedOn="{StaticResource FormTextBlockStyle}" TargetType="TextBlock" />
</UserControl.Resources>
<StackPanel>
<TextBox Name="SearchText"
Focusable="True"
Text="{utils:ValidatingLiveBinding SearchText}"
attached:NavigatingListBoxBehavior.LinkedListBox="{Binding ElementName=SearchResults}">
</TextBox>
<ScrollViewer HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Auto"
MaxHeight="400">
<ListBox Name="SearchResults"
ItemsSource="{Binding SearchResults}"
SelectedItem="{Binding SelectedSearchItem}"
Visibility="{Binding HasSearchResults, Converter={StaticResource BooleanToVisibilityConverter}}"
attached:ItemsControlProperties.DoubleClickCommand="{Binding BrowseToObjectCommand}"
KeyboardNavigation.IsTabStop="False"
IsSynchronizedWithCurrentItem="True" />
</ScrollViewer>
</StackPanel>
</UserControl>
public partial class ObjectSearchView : UserControl
{
public ObjectSearchView()
{
InitializeComponent();
this.Loaded += this.OnLoad;
}
private void OnLoad(object sender, RoutedEventArgs e)
{
this.PrintFocusInfo();
FocusManager.SetFocusedElement(FocusManager.GetFocusScope(this), this.SearchText);
this.PrintFocusInfo();
this.SearchText.Focus();
this.PrintFocusInfo();
Keyboard.Focus(this.SearchText);
this.PrintFocusInfo();
}
[Conditional("DEBUG")]
private void PrintFocusInfo()
{
var logicalElement = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this.SearchText));
Debug.WriteLine("Current logical focus is on '{0}', of type '{1}' ({2})".FormatInvariantCulture((logicalElement as FrameworkElement)?.Name, logicalElement?.GetType().Name, logicalElement));
var focusedElement = Keyboard.FocusedElement;
Debug.WriteLine(
"Current Keyboard Focus is on '{0}', of type '{1}' ({2})".FormatInvariantCulture(
(focusedElement as FrameworkElement)?.Name,
focusedElement.GetType().Name,
focusedElement));
}
}
Current logical focus is on '', of type '' ()
Current Keyboard Focus is on '', of type 'MainWindow' (UserInterface.Views.MainWindow)
Current logical focus is on '', of type '' ()
Current Keyboard Focus is on '', of type 'MainWindow' (UserInterface.Views.MainWindow)
Current logical focus is on '', of type '' ()
Current Keyboard Focus is on 'SearchText', of type 'TextBox' (System.Windows.Controls.TextBox)
Current logical focus is on '', of type '' ()
Current Keyboard Focus is on 'SearchText', of type 'TextBox' (System.Windows.Controls.TextBox)
最佳答案
这是我最终创建的行为,可以为我解决此问题。我仍然不知道为什么能正常工作的很多事情……但是,如果您在让Focus进行协作时遇到问题,那么当IsVisible设置为true时,就好像 key 在捕获它,并由Dispatcher来设置焦点为你。我将此事件链接到文本框上的IsVisibleChanged元素(通过附加的行为)。
private void SetFocusOnVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
this.Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() => this.AssociatedObject.Focus()));
}
}
关于wpf - WPF焦点,无法同时获得逻辑和键盘焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39946794/
我有 bool 数组 A 和 B,想得到 C。C 就像 A 和 B 的逻辑与,但在匹配索引时有一些回旋余地。也就是说,逻辑 AND 将执行 A[r, c] AND B[r, c],但我想要的是 A[r
我的代码中的一些断言刚开始表现得很奇怪,我想知道以前是否有人遇到过类似情况。一个简短的代码片段,如: #include class A{ protected: bool is
我希望有人可以帮助我解决以下问题,答案是最好的,但如果您能指出我正确的方向,这也会有所帮助。 我是大学最后一年的学生,这些问题来自之前关于形式方法的考试,我可以知道为今年的论文准备的答案。我们的讲师似
假设我需要为某些输入构建一个真值表,它要求我提供逻辑和、算术和和逻辑乘积。它们之间有什么区别? 最佳答案 逻辑和 - 一种计算机加法,当一个或两个输入变量为 1 时,结果为 1;当输入变量均为 0 时
我有一个过滤器,在我的例子中它是SessionFilter,它在 doFilter() 方法中有一些自定义逻辑来验证请求,在验证此请求的过程中我正在执行数据库操作。当我部署在Weblogic环境中时,
这个问题在这里已经有了答案: The output of cout << 1 && 0; (1 个回答) 关闭 3 年前。 我发现 (true && false) 给出 0,而 true && fa
C语言真/假表 我听说过C语言的表true false for and && or ||是一种数学,他们说如果 true+true=true 和 false+true=false 我对此有点困惑,我尝
我在查询中遇到 OR 逻辑问题。我正在尝试对列 (D) 求和,如果: • A 列 = cars/build-price/carselector.html • B 列 = 包含 ateca 或 ibiz
我有一个带有图像组件的表单:Image 我想获得该图像中的真实鼠标位置。 为此,我使用下面的公式,该公式位于函数中 mouseXInImage = MouseXInComponent / Compon
在 Javascript 中,我有一个 if 语句失败,但应该通过,但我不明白为什么。这是 if 的结构。 if(parseInt(obj.OptionCredit) > parseInt(Accou
我正在为应用程序问题编写一些伪代码。我想就我是否走在正确的轨道上,或者我的做法是否完全错误,提出一些建议? 我有 3 个查询条目值: 申请人年龄 配偶年龄 child 数量 对于上述每个值,我的费率表
我的输出是正确的,但我想知道 STUFF 的实际工作原理。 我有一个简单的查询,它返回 @startDate 和 @endDate 之间的总月数。 在 STUFF 的帮助下,我将那几个月存储到 @co
分离 express 应用程序的不同层的最佳方法是什么,这样我的 app.js 文件就不会塞满各种功能?我来自 Java 世界,所以我通常将模型、业务逻辑和 DAO 代码放在不同的层中。 另一个困扰我
我是一名优秀的程序员,十分优秀!