将我的对象继承绑定到ListView

时间:2015-11-13 08:14:07

标签: wpf listview bind

我尝试将我的对象绑定到ListView并且这不起作用,这个对象是从另一个继承的,所以我的怀疑是maby我尝试另一种方式或者我错过了一些东西。 这就是我的全部内容:

此对象代表WiresharkFile,我尝试支持多种格式,因此每个格式文件都具有以下属性:

  1. 文件名
  2. 包数
  3. 发送的数据包(我的应用程序也使用Pcap.Net发送此数据包)
  4. 持续时间
  5. 速度
  6. 进度(百分比)

    公共抽象类WiresharkFile {     protected string _fileName;     protected int _packets;     protected int _packetsSent;     protected string _duration;     protected double _speed;     protected int _progress;

    public string FIleName
    {
        get { return _fileName; }
        set { _fileName = value; }
    }
    
    public int Packets
    {
        get { return _packets; }
        set { _packets = value; }
    }
    
    public int PacketsSent
    {
        get { return _packetsSent; }
        set { _packetsSent = value; }
    }
    
    public string Duration
    {
        get { return _duration; }
        set { _duration = value; }
    }
    
    public double Speed
    {
        get { return _speed; }
        set { _speed = value; }
    }
    
    public int Progress
    {
        get { return _progress; }
        set { _progress = value; }
    }
    

    }

  7. 这是我的列表视图:

    <ListView ItemsSource="{Binding wiresharkFiles}" >
                        <ListView.View>
                            <GridView ColumnHeaderContainerStyle="{StaticResource ListViewHeaderStyle}">
                                <!-- file name column -->
                                <GridViewColumn Width="485" Header="File name" DisplayMemberBinding="{Binding FileName}" />
    
                                <!-- speed column -->
                                <GridViewColumn x:Name="SpeedCell"  Width="130" Header="Speed" CellTemplate="{StaticResource MyDataTemplate2}" />
    
                                <!-- duration column -->
                                <GridViewColumn Width="100" Header="Duration" DisplayMemberBinding="{Binding Duration}" />
    
                                <!-- packets column -->
                                <GridViewColumn Width="100" Header="Packets" DisplayMemberBinding="{Binding Packets, StringFormat={}{0:#,0}}" />
    
                                <!-- packet sent -->
                                <GridViewColumn Width="100" Header="Packets sent" DisplayMemberBinding="{Binding PacketsSent, StringFormat={}{0:#,0}}" />
    
                                <!-- progress column -->
                                <GridViewColumn x:Name="ProgressCell"  Width="60" Header="Progress" CellTemplate="{StaticResource MyDataTemplate}" />
                            </GridView>
                        </ListView.View>
    </ListView>
    

    我的Collection包含所有这些对象:

    声明:

    ObservableCollection<WiresharkFile> wiresharkFiles;
    

    在委托人之后:

    wiresharkFiles = new ObservableCollection<WiresharkFile>();
    this.DataContext = this;
    

    现在从我的主要内容中创建新对象:

    WiresharkFile wiresharkFile = new Libpcap("file name");  
    wiresharkFile.ReadFileDetails(); // this read the file and count how many packet contain, duration...
    wiresharkFiles.Add(wiresharkFile);
    

    Libpcap继承自WiresharkFile

    public class Libpcap : WiresharkFile...
    

    所以在这一点之后我的abject在Collection内,但我仍然无法在ListView内看到这一点。 我也试着看看try-catch如果扔掉除外但没什么。

1 个答案:

答案 0 :(得分:0)

你可以尝试

wiresharkFiles = new ObservableCollection<WiresharkFile>();
//this.DataContext = this
this.DataContext = wiresharkFiles;