WPF 滚动和焦点改变问题
我的 WPF 应用程序滚动时遇到问题。
这是交易。我的 UI 如下:
我的应用程序的作用是充当许多应用程序的中央枢纽并启动它们。管理员可以启动其他用户记录的转储。 因此,我有一个 ListView,显示应用程序列表,如果需要,该列表是可滚动的。 我定义了一个 GroupStyle
来显示扩展器并模拟 Windows 资源管理器视图。 一切正常,我只是有一个问题:当用鼠标滚轮滚动时,透明蓝色的组件(“启动模式”)似乎抓住了焦点并停止滚动。 这尤其意味着,如果我的鼠标位于此控制之外的任何位置,则滚动是可以的。但是每当鼠标进入这个控件时,我就无法再滚动了。 我尝试修改属性 Focusable
并将其设置为 False
,但没有任何改变。我猜这最终不是焦点问题。 有人知道如何避免滚动被元素捕获吗?
下面是扩展器内容的一些(简化的,删除了一些无用的属性,以便尽可能清晰)XAML:
<StackPanel Orientation="Vertical" VerticalAlignment="Top" >
<ToggleButton>
<!-- ToggleButton Content... -->
</ToggleButton>
<!-- This is the custom component in which you can see "Launch mode" -->
<my:UcReleaseChooser >
<!-- Properties there. I tried to set Focusable to False, no impact... -->
</my:UcReleaseChooser>
</StackPanel>
以及 UcReleaseChooser
的代码:
<StackPanel HorizontalAlignment="Stretch"
Focusable="False" ScrollViewer.CanContentScroll="False">
<ListBox ItemsSource="{Binding ListChosenReleases}" BorderBrush="LightGray" Background="AliceBlue"
HorizontalAlignment="Stretch" Focusable="False" ScrollViewer.CanContentScroll="False">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"
Focusable="False" ScrollViewer.CanContentScroll="False"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch"
Focusable="False" ScrollViewer.CanContentScroll="False">
<TextBlock DockPanel.Dock="Top"
HorizontalAlignment="Left" Text="{Binding Key}"
FontStyle="Italic"/>
<ListBox DockPanel.Dock="Bottom"
HorizontalAlignment="Right" ItemsSource="{Binding Value}"
BorderBrush="{x:Null}" Background="AliceBlue"
Focusable="False" ScrollViewer.CanContentScroll="False">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Focusable="False"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<-- Blah blah about style -->
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Key}" Margin="3"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBoxItem}}}"
Focusable="False" ScrollViewer.CanContentScroll="False"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
如您所见,UcReleaseChooser< /code> 包含
RadioButton
列表的列表。我尝试设置 Focusable
& CanContentScroll
到 False
在任何看起来合适的地方,但控件一直阻止主 UI 滚动...
我想我应该更改另一个属性...有什么想法吗?
谢谢!
I am having a problem with scrolling for my WPF application.
Here is the deal. My UI is the following:
The role of my application is to act as a central hub for many applications and launch them. An admin can launch a dump recorded by another user.
Therefore, I have a ListView
, showing the application list, which is scrollable if needed.
I defined a GroupStyle
in order to show expanders and emulate a Windows Explorer view.
Everything works fine, I just have a problem: when scrolling with the mouse wheel, the component in clear blue ("Launch mode") seems to be catching the focus and stop scrolling.
This means especially that if my mouse is anywhere out of this control, the scrolling is okay. But whenever the mouse enters this control, I can't scroll anymore.
I tried to modify the property Focusable
and set it to False
everywhere I could but nothing changed. I'd guess that it is finally not a focus problem.
Anybody has an idea on how to avoid the scrolling to be caught by the element?
Here is some (simplified, removed some useless properties so as to make it as clear as possible) XAML for the expander's content:
<StackPanel Orientation="Vertical" VerticalAlignment="Top" >
<ToggleButton>
<!-- ToggleButton Content... -->
</ToggleButton>
<!-- This is the custom component in which you can see "Launch mode" -->
<my:UcReleaseChooser >
<!-- Properties there. I tried to set Focusable to False, no impact... -->
</my:UcReleaseChooser>
</StackPanel>
And the code for UcReleaseChooser
:
<StackPanel HorizontalAlignment="Stretch"
Focusable="False" ScrollViewer.CanContentScroll="False">
<ListBox ItemsSource="{Binding ListChosenReleases}" BorderBrush="LightGray" Background="AliceBlue"
HorizontalAlignment="Stretch" Focusable="False" ScrollViewer.CanContentScroll="False">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"
Focusable="False" ScrollViewer.CanContentScroll="False"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch"
Focusable="False" ScrollViewer.CanContentScroll="False">
<TextBlock DockPanel.Dock="Top"
HorizontalAlignment="Left" Text="{Binding Key}"
FontStyle="Italic"/>
<ListBox DockPanel.Dock="Bottom"
HorizontalAlignment="Right" ItemsSource="{Binding Value}"
BorderBrush="{x:Null}" Background="AliceBlue"
Focusable="False" ScrollViewer.CanContentScroll="False">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Focusable="False"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<-- Blah blah about style -->
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Key}" Margin="3"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBoxItem}}}"
Focusable="False" ScrollViewer.CanContentScroll="False"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
As you can see, the UcReleaseChooser
contains a list of RadioButton
lists. I tried to set Focusable
& CanContentScroll
to False
everywhere it seemed appropriate, but the control keeps preventing the main UI to scroll...
I guess I should change another property... Any idea?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于
ListBox
,或更具体地说,是ListBox
模板中的ScrollViewer
。这是获取滚动事件并在ListView
中的外部ScrollViewer
看到它们之前消耗它们。如果可能的话,我建议用
ItemsControl
替换ListBox
。但是,这意味着不会有SelectedItem
属性。如果您需要,我建议将ScrollViewer.HorizontalScrollBarVisibility
(或VerticalScrollBarVisibility
)设置为Disabled
。如果做不到这一点,我只能建议重新模板化ListBox
以使其根本不包含ScrollViewer
。The problem is the
ListBox
, or more specifically, theScrollViewer
within theListBox
's template. This is getting your scroll events and consuming them before the outerScrollViewer
in theListView
even sees them.I would advise replacing the
ListBox
with anItemsControl
if possible. However, that implies there will be noSelectedItem
property. If you need that, I would suggest settingScrollViewer.HorizontalScrollBarVisibility
(orVerticalScrollBarVisibility
) toDisabled
. Failing that, I can only suggest re-templatingListBox
to not contain aScrollViewer
at all.我遇到了滚动查看器内的列表框窃取焦点的问题(滚动查看器内有多个列表框)。因此,我创建了一个附加属性,它拒绝列表框滚动的能力。因此,包含列表框的滚动查看器可以滚动
您的控件是一个列表框,因此应该按原样工作,但没有理由将扩展限制为列表框;这只是为了符合我的确切目的。
然后在您的 XAML 中,您只需将其放在列表框中:
当然,请记住将命名空间包含在 XAML 顶部的扩展中:
I had an issue with a listbox stealing focus inside a scrollviewer (I have multiple listboxes inside the scrollviewer). So I created an attached property which denies the listbox the ability to scroll. So therefore the scrollviewer which is housing the listbox can scroll
Your control is a listbox, so this should work as is, but there is no reason the Extension should be limited to a Listbox; it just is to match my exact purpose.
And then in your XAML, you just put this on the listbox:
Of course, remembering to include the namespace to your extensions in the top of the XAML: