簡體   English   中英

C# WPF 更改列表框項目大小

[英]C# WPF Change Listbox Item Size

我想創建 Metro 風格的列表框。 我需要 Listboxitem 的 XAML 樣式代碼。 我想更改 ListboxItem 大小運行時。

您可以使用樣式。 將以下 XAML 添加到窗口:

<Grid>
    <ListBox Name="MyListBox" DisplayMemberPath="ItemText">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Height" Value="{Binding ItemHeight}"/>
                <Setter Property="Width" Value="{Binding ItemWidth}"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

在后面的代碼中,通過將一些數據項綁定到列表框來填充列表,如下所示:

    public class ItemData
    {
        public int ItemHeight { get; private set; }
        public int ItemWidth { get; private set; }
        public string ItemText { get; private set; }
        public ItemData(int itemHeight, int itemWidth, string itemText)
        {
            ItemHeight = itemHeight;
            ItemWidth = itemWidth;
            ItemText = itemText;
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        MyListBox.ItemsSource = BuildItems();
    }

    private System.Collections.IEnumerable BuildItems()
    {
        yield return new ItemData(20, 200, "First");
        yield return new ItemData(40, 300, "Second");
        yield return new ItemData(60, 100, "Third");
    }

當您運行該應用程序時,您會看到不同的項目具有不同的尺寸。

暫無
暫無

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

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