我正在尝试获得具有多个选择的TreeView
。我必须以编程方式构建TreeView
(我假设),因为我从Database
获取数据。
我的问题是 shown here as I can't embed pictures yet.
该程序在情况A中按预期工作,我将鼠标悬停在TreeView
上 - 我可以向上和向下滚动。
然而,在情况B中,这是不可能的,我正在徘徊ListBox
。
示例的XAML如下:
<Window x:Class="WPF_test_1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="150" Width="230">
<Grid>
<TreeView x:Name="treeView" Margin="10,10,10,9.8"/>
</Grid>
</Window>
和C#:
using System.Windows;
using System.Windows.Controls;
namespace WPF_test_1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ListBox lb = new ListBox();
lb.Items.Add("Test with a long name");
lb.Items.Add("Test");
lb.Items.Add("Test");
lb.Items.Add("Test");
lb.Items.Add("Test");
lb.Items.Add("Test");
lb.SelectionMode = SelectionMode.Extended;
lb.BorderThickness = new Thickness(0);
TreeViewItem lv = new TreeViewItem();
lv.Header = "TEST";
lv.Items.Add(lb);
treeView.Items.Add(lv);
}
}
}
我选择向ListBox
添加TreeViewItem
,因为这样可以进行多项选择而无需实施TreeView
的扩展 - 但正如我所说,它不允许我要正确滚动。
有没有人知道解决方法,或问题可能是什么?
提前谢谢。
答案 0 :(得分:0)
现在我已经通过我的头骨了解了实际问题是什么,我发现a way to disable scrolling on a ListBox
。
这是一个纯XAML快速演示,在TreeViewItems
的孩子们的一组列表框中演示该技巧。
在纯代码隐藏中,生成模板有点麻烦。我建议在XAML中创建样式,给它x:Key="NonScrollingListBoxStyle"
,并在代码隐藏中加载FindResource("NonScrollingListBoxStyle")
。然后,您可以手动将其应用于每个ListBox
。在这里,我将其设为implicit style,只是为了节省打字。
<TreeView Height="120">
<TreeView.Resources>
<Style TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ItemsPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
<TreeViewItem IsExpanded="True" Header="Item 1">
<ListBox>
<TextBlock>Item A</TextBlock>
<TextBlock>Item B</TextBlock>
<TextBlock>Item C</TextBlock>
<TextBlock>Item D</TextBlock>
<TextBlock>Item E</TextBlock>
</ListBox>
</TreeViewItem>
<TreeViewItem IsExpanded="True" Header="Item 2">
<ListBox>
<TextBlock>Item A</TextBlock>
<TextBlock>Item B</TextBlock>
<TextBlock>Item C</TextBlock>
<TextBlock>Item D</TextBlock>
<TextBlock>Item E</TextBlock>
</ListBox>
</TreeViewItem>
<TreeViewItem IsExpanded="True" Header="Item 3">
<ListBox>
<TextBlock>Item A</TextBlock>
<TextBlock>Item B</TextBlock>
<TextBlock>Item C</TextBlock>
<TextBlock>Item D</TextBlock>
<TextBlock>Item E</TextBlock>
</ListBox>
</TreeViewItem>
<TreeViewItem IsExpanded="True" Header="Item 4">
<ListBox>
<TextBlock>Item A</TextBlock>
<TextBlock>Item B</TextBlock>
<TextBlock>Item C</TextBlock>
<TextBlock>Item D</TextBlock>
<TextBlock>Item E</TextBlock>
</ListBox>
</TreeViewItem>
</TreeView>