是否有可能(默认ListBox
包含)和with ?我想实现下一个视图:
ScrollViewer
ListBoxItem
ScrollViewer
而且这个 ListBox 也应该支持虚拟化。(我知道如何启用它,我只是想知道如果我使用 2 个滚动查看器它会起作用吗?)
更新:
我需要使用ListBox.ItemTemplate
,因为我正在绑定ItemsSource
.
感谢您的提示。
是否有可能(默认ListBox
包含)和with ?我想实现下一个视图:
ScrollViewer
ListBoxItem
ScrollViewer
而且这个 ListBox 也应该支持虚拟化。(我知道如何启用它,我只是想知道如果我使用 2 个滚动查看器它会起作用吗?)
更新:
我需要使用ListBox.ItemTemplate
,因为我正在绑定ItemsSource
.
感谢您的提示。
简单的。(我给了你三种方法,一定是对的!)
通过 Xaml:
<ListBox x:Name="ListBoxControl" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="520">
<ListBoxItem Width="520">
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled">
<Label Content="My Name is KidCode. This is a reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaaaly long comment."/>
</ScrollViewer>
</ListBoxItem>
</ListBox>
从 C#:
namespace StackExchange
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var lbv = new ListBoxItemSV()
{
Height = 40,
Width= 520,
Background = Brushes.Blue
};
ListBoxControl.Items.Add(lbv);
}
public class ListBoxItemSV : ListBoxItem
{
ScrollViewer sv = new ScrollViewer()
{
HorizontalScrollBarVisibility = ScrollBarVisibility.Visible,
VerticalScrollBarVisibility = ScrollBarVisibility.Hidden
};
Label lbl = new Label()
{
Content = "A really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really long name."
};
public ListBoxItemSV()
{
sv.Content = lbl;
this.Content = sv;
}
}
}
}
这将导致以下结果:(我添加了一个简短的注释,以便您可以看到差异,您可以让滚动条一直穿过,但这只是一个示例。)
如果您使用项目模板:( 我从来没有这样做过,所以如果它错了请不要拍我!:))
XAML:
<ListBox x:Name="ListBoxTwo">
<ListBox.ItemTemplate>
<DataTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Width="520">
<Label Content="{Binding}"/>
</ScrollViewer>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
代码背后:
List<string> Mylist = new List<string>();
Mylist.Add("Short Name");
Mylist.Add("Another Short Name");
Mylist.Add("A massively, hugely, giganticly, monstorously large name. (Its even bigger than than you think...............) ");
ListBoxTwo.ItemsSource = Mylist;
输出: