Windows Phone 8.1 Binding 数据绑定 增加,删除 ListView

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,60,0,28">
    <Button Content="增加新的项目" Click="AddButton_Click"></Button>
    <Button Content="删除选中的项目" Click="DelButton_Click"></Button>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListView x:Name="list">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Margin="10">
                    <TextBlock Text="{Binding OrderID}" FontSize="30"/>
                    <TextBlock Text="{Binding OrderName}" FontSize="30" Width="280"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>
public sealed partial class MainPage: Page {
    ObservableCollection < OrderModel > OrderModels = new ObservableCollection < OrderModel > ();
    public MainPage() {
        this.InitializeComponent();
        list.ItemsSource = OrderModels;
    }

    private void AddButton_Click(object sender, RoutedEventArgs e) {
        Random random = new Random();
        OrderModels.Add(new OrderModel {
            OrderID = random.Next(1000),
            OrderName = "OrderName" + random.Next(1000)
        });
    }

    private void DelButton_Click(object sender, RoutedEventArgs e) {
        if (list.SelectedItem != null) {
            OrderModel orderModel = list.SelectedItem as OrderModel;
            if (OrderModels.Contains(orderModel)) {
                OrderModels.Remove(orderModel);
            }
        }
    }

    protected override void OnNavigatedTo(NavigationEventArgs e) {}
}

public class OrderModel {
    public int OrderID {
        get;
        set;
    }
    public string OrderName {
        get;
        set;
    }
}

网友评论

无吐槽,不精彩,期待您的参与。

吐槽几句

"(必填)"

"(选填)"

"(必填,不公开)"

(必填,最多100个字符。)

先说这么多吧