如何从特殊行填充列

时间:2013-04-30 12:24:50

标签: c# wpf listview

我在wpf中生成了Listview

见这里:

<ListView x:Name="lstVCOM" Height="450" Margin="10,10,10,0" VerticalAlignment="Top">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="25" Header="Slot" DisplayMemberBinding="{Binding Path=Slot}" />
                    <GridViewColumn Width="140" Header="Messung" DisplayMemberBinding="{Binding Path=MessungNr}" />
                    <GridViewColumn Width="140" Header="Minimal" DisplayMemberBinding="{Binding Path=Min}"/>
                    <GridViewColumn Width="140" Header="Maximal" DisplayMemberBinding="{Binding Path=Max}"/>
                    <GridViewColumn Width="140" Header="Aktuelle Messung" DisplayMemberBinding="{Binding Path=MessungAkt}"/>
                    <GridViewColumn Width="140" Header="Durchschnitt" DisplayMemberBinding="{Binding Path=Durschnitt}"/>
                </GridView>
            </ListView.View>
        </ListView>

我想逐步填写示例,这意味着我想首先填充Slot列然后填充Messung列然后.....所有在第1行然后转到下一行并执行相同操作。

我该怎么做?

lstVCOM.item.add?

THX

1 个答案:

答案 0 :(得分:0)

如果您有object代表一行,我将其命名为VCOM,那么您可以创建List<T>来表示VCOM s的集合

public class VCOM {
    public int Slot { get; set; }
    public string MessungNr { get; set; }
    public decimal Min { get; set; }
    public decimal Max { get; set; }
    public string MessungAkt { get; set; }
    public decimal Durschnitt { get; set; }
}

private List<VCOM> _ListVCOM = new List<VCOM>();
public List<VCOM> ListVCOM {
    get {
    return _ListVCOM;
    }
    set {
    _ListVCOM = value;
    OnPropertyChanged("ListVCOM");
    }
}

实施INotifyPropertyChanged,以便在向VCOM添加新的List<VCOM>对象时更新您的DataGrid。

ListVCOM.Add(new VCOM() {
    Slot = 1,
    MessungNr = "Messung",
    Min = 0.0M,
    Max = 1.0M,
    MessungAkt = "Aktuelle Messung",
    Durschnitt = 0.5M,
});

要手动设置各个属性,您可以根据对Slot属性的了解来检索对象并设置其他属性(假设它是唯一的)。

int currentSlotNum = 1;
ListVCOM.Where(a => a.Slot.Equals(currentSlotNum)).First().Min = 1.0M;

然后,当您想要添加新的VCOM对象时,只需调用上面的Add Method,它就会使用新行进行更新。

    <ListView x:Name="lstVCOM" Height="450" Margin="10,10,10,0" VerticalAlignment="Top" DockPanel.Dock="Top" 
              ItemsSource="{Binding ListVCOM, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="25" Header="Slot" DisplayMemberBinding="{Binding Path=Slot}" />
                <GridViewColumn Width="140" Header="Messung" DisplayMemberBinding="{Binding Path=MessungNr}" />
                <GridViewColumn Width="140" Header="Minimal" DisplayMemberBinding="{Binding Path=Min}"/>
                <GridViewColumn Width="140" Header="Maximal" DisplayMemberBinding="{Binding Path=Max}"/>
                <GridViewColumn Width="140" Header="Aktuelle Messung" DisplayMemberBinding="{Binding Path=MessungAkt}"/>
                <GridViewColumn Width="140" Header="Durchschnitt" DisplayMemberBinding="{Binding Path=Durschnitt}"/>
            </GridView>
        </ListView.View>
    </ListView>

以下是DataGrid,具有适当的ItemsSource绑定。