0

ListBox(デフォルトで含まScrollViewerれています)とListBoxItem一緒に持つことは可能ScrollViewerですか?次のビューを実現したい: ここに画像の説明を入力

また、この ListBox は仮想化もサポートする必要があります。(有効にする方法は知っていますが、2 つのスクロール ビューアーを使用すると機能するのでしょうか?)

更新:ListBox.ItemTemplateをバインドしているため、 を使用する必要がありますItemsSource

ヒントをありがとう。

4

1 に答える 1

1

簡単。(これを行うための 3 つの方法を紹介しましたが、そのうちの 1 つが正しいに違いありません!)

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;
            }
        }
    }
}

この結果は次のようになります: (違いがわかるように短いコメントを追加しました。おそらく、スクロール バーを最後まで移動させることができますが、これは単なる例です。)

ScrollViewer を使用した ListBoxItem

アイテム テンプレートを使用している場合: (私はこれを行ったことがないので、間違っていても私を撃たないでください! :) )

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;

出力:

出力

于 2015-06-04T12:23:42.267 に答える