0

我有一个像这样的简单列表视图

<ListView Name="CoordinateList" Margin="2"
                  Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Nr}"  Header="Nr." Width="40"/>
            <GridViewColumn DisplayMemberBinding="{Binding X}" Header="X" Width="100"/>
            <GridViewColumn DisplayMemberBinding="{Binding Y}" Header="Y" Width="100"/>
            <GridViewColumn DisplayMemberBinding="{Binding Q}" Header="Q" Width="40"/>
        </GridView>
    </ListView.View>
</ListView>

我可以添加项目、排序或删除它们,例如:

CoordinateList.Items.Add(new Coordinate() { Nr = 1, X = 230, Y = 530, Q = 2 });

但是我现在如何访问或更改数据?这并没有给我以下属性Coordinate

CoordinateList.Items[i].
4

2 回答 2

0

ListView 将其Items集合存储为对象,因此为了将项目作为Coordinate对象访问,您需要Coordinate先将其转换为类型。例如:

var coord = (Coordinate) CoordinateList.Items[i];
...

编辑:另请参阅@Muds 的答案以获得更好的实践。

于 2015-10-20T08:26:37.313 回答
0

我建议您将 ListView 与集合绑定,然后从该集合中添加删除或访问对象。

不建议依赖 UI 控件从后面的代码进行数据操作。

<ListView Name="CoordinateList" ItemsSource="{Binding MyNewList}" Margin="2"
                  Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Nr}"  Header="Nr." Width="40"/>
            <GridViewColumn DisplayMemberBinding="{Binding X}" Header="X" Width="100"/>
            <GridViewColumn DisplayMemberBinding="{Binding Y}" Header="Y" Width="100"/>
            <GridViewColumn DisplayMemberBinding="{Binding Q}" Header="Q" Width="40"/>
        </GridView>
    </ListView.View>
</ListView>

然后,

public ObservableCollection<Coordinate> MyNewList { get; set;}

MyNewList.Add(new Coordinate() { Nr = 1, X = 230, Y = 530, Q = 2 })

MyNewList[I] --> will have your item. 
于 2015-10-20T08:30:39.390 回答