首页 > 代码库 > 自定义空间:listbox_listitem

自定义空间:listbox_listitem

image

<Window x:Class="MyWpfApplication.Window2"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Window2" Height="300" Width="500">    <Window.Resources>        <Style x:Key="SearchListBoxItemStyle"  TargetType="{x:Type ListBoxItem}">            <Setter Property="Template">                <Setter.Value>                    <ControlTemplate>                        <Border  x:Name="myBorder"  Height="60" BorderBrush="#ddd9cd" BorderThickness="1 1 1 1" Padding="3">                            <Grid>                                <Grid.RowDefinitions>                                    <RowDefinition />                                    <RowDefinition />                                </Grid.RowDefinitions>                                <Grid.ColumnDefinitions>                                    <ColumnDefinition/>                                    <ColumnDefinition Width="50"/>                                    <ColumnDefinition Width="20"/>                                </Grid.ColumnDefinitions>                                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=FileName}" Height="30" HorizontalAlignment="Left"/>                                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=UploadStatus}" Height="30" HorizontalAlignment="Left"/>                                <Image MouseLeftButtonDown="Image_MouseEnter" Grid.Row="0" Tag="{Binding}" Grid.Column="2" Source="Images/1.png" Height="16" Width="16" HorizontalAlignment="Right"/>                                <ProgressBar Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Value="{Binding Path=UploadValue}" Height="10"/>                            </Grid>                        </Border>                    </ControlTemplate>                </Setter.Value>            </Setter>        </Style>    </Window.Resources>        <Grid>        <Grid.RowDefinitions>            <RowDefinition Height="*"></RowDefinition>            <RowDefinition Height="*"></RowDefinition>            <RowDefinition Height="6*"></RowDefinition>        </Grid.RowDefinitions>        <Button Grid.Row="0" Content="删除文件" Name="but_del" Click="but_del_Click" ></Button>        <Button Grid.Row="1" Content="选择文件" Name="selectFile" Click="selectFile_Click"></Button>        <ListBox Grid.Row="2" x:Name="ListBox1" ScrollViewer.VerticalScrollBarVisibility="Visible" HorizontalAlignment="Stretch"                  ItemContainerStyle="{DynamicResource SearchListBoxItemStyle}" ScrollViewer.HorizontalScrollBarVisibility="Auto" />    </Grid></Window>

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;using Microsoft.Win32;namespace MyWpfApplication {    /// <summary>    /// Window2.xaml 的交互逻辑    /// </summary>    public partial class Window2 : Window {        List<TempUploadFile> lst = new List<TempUploadFile>();        public Window2() {            InitializeComponent();        }        public class TempUploadFile        {            public string FileName { get; set; }            public string UploadStatus { get; set; }            public double UploadValue { get; set; }        }        private void but_del_Click(object sender, RoutedEventArgs e)        {            if (lst.Count>0)            {                lst.RemoveRange(0, lst.Count);                this.ListBox1.ItemsSource = null;                this.ListBox1.ItemsSource = lst;            }        }        private void selectFile_Click(object sender, RoutedEventArgs e)        {            System.Windows.Forms.OpenFileDialog openfiledialog = new System.Windows.Forms.OpenFileDialog();             openfiledialog.Multiselect = true;            if (openfiledialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {                string[] filenames = openfiledialog.FileNames;                foreach (string file in filenames)                {                    TempUploadFile a1 = new TempUploadFile                    {                        FileName = file,                        UploadStatus = "正在上传",                        UploadValue = 30d                    };                    lst.Add(a1);                }                this.ListBox1.ItemsSource = null;                this.ListBox1.ItemsSource = lst;            }        }        private void Image_MouseEnter(object sender, MouseButtonEventArgs e)        {            Image image = (Image)sender;            lst.Remove((TempUploadFile)image.Tag);                         this.ListBox1.ItemsSource = null;            this.ListBox1.ItemsSource = lst;        }    }}

自定义空间:listbox_listitem