is it possible to have ListBox
(which contains ScrollViewer
by default) and ListBoxItem
with ScrollViewer
?
I want to achieve next view:
And this ListBox should also support virtualization. (I know how enable it, I just wondering will it work if I use 2 scroll viewers?)
UPDATED:
I need to use ListBox.ItemTemplate
, since I'm binding ItemsSource
.
Thanks for tips.
Easy. (I've given you three ways to do this, one must be right!)
Through 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>
From 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;
}
}
}
}
This results in the following: (I've added a short comment just so you can see the difference, you can probably make the scroll bar go all the way across, but this is just an example.)
If your using Item Template: (I've never done this, so don't shoot me if its wrong! :) )
XAML:
<ListBox x:Name="ListBoxTwo">
<ListBox.ItemTemplate>
<DataTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" Width="520">
<Label Content="{Binding}"/>
</ScrollViewer>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code Behind:
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;
Output: