[英]C# WPF Change Listbox Item Size
I wanna create metro style listbox.我想创建 Metro 风格的列表框。 I need XAML style code for Listboxitem.我需要 Listboxitem 的 XAML 样式代码。 I wanna change Listboxİtem size runtime.我想更改 ListboxItem 大小运行时。
You can use styles.您可以使用样式。 Add the following XAML to a Window:将以下 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>
In the code behind, populate the list by binding some data items to the list box like this:在后面的代码中,通过将一些数据项绑定到列表框来填充列表,如下所示:
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");
}
When you run the app you will see that the different items have different sizes.当您运行该应用程序时,您会看到不同的项目具有不同的尺寸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.