簡體   English   中英

如何編碼ListBox的方向-WPF-C#

[英]How to code Orientation of ListBox - WPF - c#

我需要在C#代碼中將Orientation設置為ListBox。 我需要像以下XAML代碼一樣的結果:

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

我有這個:

ListBox elementListBox = new ListBox();
StackPanel pomocnyStackPanel = new StackPanel();
pomocnyStackPanel.Orientation = Orientation.Horizontal;

如何添加“ ItemsPanel”?

以前可以使用FrameworkElementFactory來完成此操作,但是現在不建議使用,建議使用XamlReader代替:

elementListBox.ItemsPanel = (ItemsPanelTemplate)XamlReader.Parse("<ItemsPanelTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><StackPanel Orientation=\"Horizontal\"/></ItemsPanelTemplate>");

我不確定100%,但是我認為您想要這樣的東西:

FrameworkElementFactory factory = new FrameworkElementFactory(typeof(StackPanel));
factory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
ItemsPanelTemplate itemsPanelTemplate = new ItemsPanelTemplate(factory);
ListBox elementListBox = new ListBox();
elementListBox.ItemsPanel = itemsPanelTemplate;

更新>>>

是的,我剛剛對其進行了測試,它可以按預期運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM