Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C WPF更改列表框项目大小_C#_Wpf - Fatal编程技术网

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

C# C WPF更改列表框项目大小,c#,wpf,C#,Wpf,我想创建metro风格的列表框。我需要Listboxitem的XAML样式代码。我想更改Listboxİtem运行时的大小 您可以使用样式。将以下XAML添加到窗口: <Grid> <ListBox Name="MyListBox" DisplayMemberPath="ItemText"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxIte

我想创建metro风格的列表框。我需要Listboxitem的XAML样式代码。我想更改Listboxİtem运行时的大小

您可以使用样式。将以下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>

运行应用程序时,您将看到不同的项目具有不同的大小。

投票否决此答案的人是否可以解释为什么无法在运行时实现更改ListBoxItem大小的目标?@Vishera,如果您喜欢我的答案,请接受。我认为它给了你实现目标所需的洞察力。
    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");
    }