如何使用c以编程方式在自定义列表框中添加新项

本文关键字:列表 添加 新项 自定义 何使用 编程 方式 | 更新日期: 2023-09-27 18:28:52

我有一个自定义的列表框,它是我用Microsoft Expression Blend创建的,包含三个项目(一个图像、一个名称和一个文本)。。这就是ListBox模板的代码!!

<DataTemplate x:Key="ListBoxTemplate">
    <Grid Width="320" Height="80" Background="#FFCAE5DE">
        <Grid.RowDefinitions>
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="70"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Path Grid.ColumnSpan="1" Data="M8.4999981,6.8212103E-13 L256.5,6.8212103E-13 C261.19443,-1.8775456E-06 265,3.8055778 265,8.4999981 L265,55.499998 C265,60.194418 261.19443,63.999998 256.5,63.999998 L52.689537,63.999998 52.702888,75.988852 45.902157,63.999998 8.4999981,63.999998 C3.8055797,63.999998 0,60.194418 0,55.499998 L0,8.4999981 C0,3.8055778 3.8055797,-1.8775456E-06 8.4999981,6.8212103E-13 z" 
                          Fill="#FF3E977D" Height="Auto" Margin="5,5,8,-11" StrokeStartLineCap="Round" Stretch="Fill" StrokeEndLineCap="Round" StrokeDashCap="Round" Stroke="Black" StrokeThickness="0" 
                          StrokeLineJoin="Round" VerticalAlignment="Stretch" Grid.Column="1" Opacity="0.535" />
        <Border Margin="10,5,0,-2" BorderBrush="Black" BorderThickness="1" Height="Auto" CornerRadius="4">
            <Image Source="{Binding Path=ImageURL}" Stretch="Fill" Width="Auto" Height="Auto" />
        </Border>
        <StackPanel Orientation="Horizontal" TextBlock.FontWeight="Bold" Height="Auto" Width="Auto" Grid.Column="1" Margin="5,5,0,-11">
            <TextBlock Text="{Binding Path=Text}" Width="400" Margin="3,2,0,16" Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" TextAlignment="Justify" TextWrapping="Wrap" ScrollViewer.HorizontalScrollBarVisibility="Auto"/>
        </StackPanel>
        <StackPanel Margin="0,47,43.5,-51" Grid.Column="1" Height="Auto" HorizontalAlignment="Stretch" Width="Auto" Grid.Row="1">
            <TextBlock Text="{Binding Path=Name}" Height="20" Margin="60,20,8,0" Width="Auto"/>
        </StackPanel>
    </Grid>
</DataTemplate>

正如您所看到的,我有三个绑定(ImageURL、Text和Name)。现在要做的是在C#中实现一个方法,允许我在这个列表框中添加一个新项。首先,我发现了一些用c#添加lisboxitem的例子,但我不知道如何将其应用于我的自定义列表框。。。c中的方法是这样的!

private ListBoxItem AddItem(string imageUrl, string text, string name)
{ 
    ListBoxItem item = new ListBoxItem();
    /* the
       code
       of 
       the 
       customized
       ListBox */
     item.content = content;
     return item;
}

问题是,当我调用方法AddItem(imageUrl, text, name)时,如何在自定义列表框中添加新项目?

任何想法。。任何帮助都将不胜感激。

@Margnus Johnasson非常感谢您的解决方案。但是仍然存在一个问题。。如果我想添加多个项目,该怎么办?!!这是我实现的方法。。

public void Additem(string imageUrl, string text, string name)
    {
        ObservableCollection<Person> Items = new ObservableCollection<Person>();
        MyListBox.ItemsSource = Items;
        Items.Add(new Person() { Image = imageUrl, Text = text, Names = name });
    }

这个方法只添加一个项目,即使我多次调用它,但我的问题是,每次调用方法时,我都想在列表框中添加一个新项目

Additem(string imageUrl, string, text, string name)

而不覆盖其他项目!!

感谢您的理解。

如何使用c以编程方式在自定义列表框中添加新项

你真的应该看看数据绑定,它会有很大帮助。

基础是创建一个表示项目的模型:

public class MyItem
{
  public string ImageUrl { get; set; }
  public string Text { get; set; }
  public string Name { get; set; }
}

然后创建这些模型项的可绑定集合:

ObservableCollection<MyItem> Items{get;set;}

并将您的ListView绑定到此集合:

Items = new ObservableCollection<MyItem>();
myListBox.ItemsSource = Items;

最后,你可以添加这样的项目:

Items.Add(new MyItem() { ImageUrl = "http://..", Text = "My Text", Name = "My Name" });

由于强大的数据绑定,ListBox应该接收您对集合所做的更改,并且您应该在ListBox中看到新项。