【问题标题】:I want to put the image and text into the list box item(C# wpf)我想将图像和文本放入列表框项(C# wpf)
【发布时间】:2019-06-27 04:45:26
【问题描述】:

列表框项的图片太大。enter image description here

我想让它成为一个恒定高度的项目。就像下图一样。 enter image description here

这是我的代码

xaml:

<ListBox Grid.Row="0" x:Name="listBox" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" AllowDrop="True" Drop="ListBox_Drop" DragEnter="ListBox_DragEnter" ScrollViewer.VerticalScrollBarVisibility="Visible" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <Image Margin="3" Source="{Binding Path}"/>
                    <TextBlock Margin="3" Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

cs:

class VideoListing
{
    public string Name { get; set; }
    public string Path { get; set; }
}
List<VideoListing> list = new List<VideoListing>();
    public VideoPanel()
    {
        InitializeComponent();
        list.Add(new VideoListing()
        {
            Name = "hello",
            Path = @"C:\Users\jskae\Desktop\Screenshot.png",
        });
        listBox.Items.Add(list);
    }

【问题讨论】:

  • 您的目标是什么:Winforms、WPF、ASP..?您应该始终正确标记您的问题,以便人们可以在问题页面上看到它!
  • @TaW 谢谢,我更改了问题标题。
  • WPF 不应出现在标题中。应该是标签! (已更改)

标签: c# image text listbox wpf-controls


【解决方案1】:

您需要在 Image 控件上设置 Height 或 MaxHeight 属性。既然你说你想要一个“恒定高度”,那么设置图像的高度并告诉它适当地缩放源。

<StackPanel Orientation="Vertical">
    <Image Margin="3" Source="{Binding Path}" Height="64" Stretch="Uniform"/>
    <TextBlock Margin="3" Text="{Binding Name}"/>
</StackPanel>

【讨论】:

    【解决方案2】:

    在您的应用程序中使用 listView 控件并使用 imagelist 控件来保存许多图像并在 listview 控件中设置图像索引。 我觉得太简单了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多