我正在编写一个带有允许多项选择的列表框的应用程序(SelectionMode=Multiple); lisbox 中的项目是食谱的成分。
不幸的是,单击列表框项目会选择该项目,这可能是不需要的。我想要以下场景:
- 用户单击列表框以选择列表框(列表框本身,而不是项目)
- 用户滚动到正确的项目并选择它
我所做的是设置 ListBoxItem 的样式以包含一个复选框和一个 ContentPresenter (例如在这个博客中 http://alexshed.spaces.live.com/blog/cns!71C72270309CE838!133.entry)。
不过,单击成分名称即可将其选中。
因此,我在包含成分名称的文本块上捕获 MouseDown 事件,找到基础 ListBoxItem,对其调用 Focus() 并将事件的 Handled 属性设置为 true。
现在,列表框项目具有焦点但未被选中。使用向上和向下键表明焦点位于正确的项目上。
我的问题是用户看不到他点击了正确的项目。该项目上未显示虚线矩形。
结果如下:
这就是我想要的:
我尝试过调用私有 WPF 方法,例如 KeyboardNavigation.ShowFocusVisual,我尝试过将击键发送到列表框(当由人类完成时,按右光标键或 Alt 键会出现虚线矩形)。
任何想法 ?
SendInput 是我发现的唯一可以克服这个问题的方法。从this http://robrelyea.wordpress.com/2007/06/15/sendkeys-for-wpf/ link.
PInvoke 到 SendInput – 这是
模拟输入的官方方式。它
将输入推入所有的
预期的代码路径,并且是
与真实输入无法区分。
使用它的一个简单方法是输入模拟器 http://inputsimulator.codeplex.com/来自 CodePlex。
添加对InputSimulator.dll的引用我们可以做这样的事情
private bool m_waitingForFocusVisualStyle = false;
private void ListBoxItem_GotFocus(object sender, RoutedEventArgs e)
{
if (m_waitingForFocusVisualStyle == false)
{
m_waitingForFocusVisualStyle = true;
InputSimulator.SimulateKeyDown(VirtualKeyCode.TAB);
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.TAB);
}
else
{
m_waitingForFocusVisualStyle = false;
}
}
但由于很多原因,这可能并不理想(例如,按 Shift+Tab 切换到 ListBoxItem)
更好的想法可能是删除 ListBoxItem 的 FocusVisualStyle 并在 ControlTemplate 中添加您自己的样式,如下所示。 (从 Blend 复制并从标准 FocusVisualStyle 添加“FocusVisualStyle”)
<ListBox ...>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template" Value="{StaticResource ListBoxItemTemplate}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<ControlTemplate x:Key="ListBoxItemTemplate" TargetType="{x:Type ListBoxItem}">
<Grid>
<Rectangle Grid.ZIndex="1"
Name="focusVisualStyle"
StrokeThickness="1"
Stroke="Black"
StrokeDashArray="1 2"
SnapsToDevicePixels="true"
Visibility="Hidden"/>
<Border x:Name="Bd"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="focusVisualStyle" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)