您的位置:首页 > 其它

ObservableCollection<T>在window phone7中的应用

2012-11-06 17:01 399 查看
ObservableCollection<T>是在WPF中出现的一个动态接口.官方文档中说:ObservableCollection是动态数据集合并且当集合中新增、修改或者删除项目时,或者集合被刷新时,都有通知机制(通过实现接口INotifyCollectionChanged)

window phone 7是继承自SilverLight,而SilverLight又继承自WPF,所以在WP7中也有这个动态接口.我在做一个列表绑定的时候,想时时同步数据,而不重新再读取一遍数据.所以就用到了它.

这里说明一点,我在使用它的时候,发展一个问题,在使用ObservableCollection绑定到listbox之后,使用remove移处数据的时候,它只是移除了ObservableCollection里的项,而没有真实的删除数据库里的值.但是却做到了.不用重新加载.

这里要说明一点,这个接口,只能在添加和删除的时候才会看到变化,而在修改的时候,不会有变化

下面上代码.

一,你要把你的实体类,实现INotifyCollectionChanged接口

public class BillingBookMerchant : INotifyPropertyChanged
{
//标识种子
private int _merchantid;

/// <summary>
/// 标识种子
/// </summary>
[Column(IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)]
public int MerchantId
{
get { return _merchantid; }
set
{
if (value != _merchantid)
{
_merchantid = value;
NotifyPropertyChanged("MerchantId");
}
}
}

//商家名称
private string _merchantname;

/// <summary>
/// 商家名称
/// </summary>
[Column(CanBeNull = false, DbType = "NVarChar(20) NOT NULL")]
public string MerchantName
{
get { return _merchantname; }
set
{
if (value != _merchantname)
{
_merchantname = value;
NotifyPropertyChanged("MerchantName");
}
}
}

//最后使用时间
private DateTime _datetimes;

/// <summary>
/// 最后使用时间
/// </summary>
[Column(DbType = "DateTime")]
public DateTime DateTimes
{
get { return _datetimes; }
set
{
if (value != _datetimes)
{
_datetimes = value;
NotifyPropertyChanged("DateTimes");
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)
{

if (PropertyChanged != null)
{

PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

}

}

}


前台代码;

<phone:PhoneApplicationPage xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
x:Class="BillingBook.BillingBookMerchantList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,0" Height="50" Orientation="Horizontal">
<HyperlinkButton x:Name="hlback" Content="返回" />
<TextBlock Text="设置->商户" Margin="100,0" VerticalAlignment="Center"></TextBlock>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0">
<!--<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Hold="GestureListener_Hold"></toolkit:GestureListener>
</toolkit:GestureService.GestureListener>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Width="200">
<toolkit:MenuItem Header="编辑" Click="MenuItem_Click"></toolkit:MenuItem>
<toolkit:MenuItem Header="删除" Click="MenuItem_Click_1"></toolkit:MenuItem>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>-->
<ListBox Height="auto" x:Name="lbMerchantList" VerticalAlignment="Top" Width="460">

<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Height="100">
<Image Width="40" VerticalAlignment="Center" Height="40" Source="/myIcons/Merchant.png" />
<TextBlock Margin="20,0"  VerticalAlignment="Center" Text="{Binding MerchantName,Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>

<!--Sample code showing usage of ApplicationBar-->
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/SystemImg/appbar.add.rest.png" Text="添加" Click="ApplicationBarIconButton_Click"/>
<shell:ApplicationBarIconButton IconUri="/SystemImg/appbar.edit.rest.png" x:Name="btnEdit" Text="修改" Click="btnEdit_Click" />
<shell:ApplicationBarIconButton IconUri="/SystemImg/appbar.delete.rest.png" Text="删除" Click="ApplicationBarIconButton_Click_1"/>
<!--<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>-->
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

</phone:PhoneApplicationPage>


后台:我只是在删除功能中用到它

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;

namespace BillingBook
{
public partial class BillingBookMerchantList : PhoneApplicationPage
{
//初始化
ObservableCollection<BillingBookMerchant> obsBBM = new ObservableCollection<BillingBookMerchant>();

BillingBookDbBll bll = new BillingBookDbBll();

public BillingBookMerchantList()
{
InitializeComponent();
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//base.OnNavigatedTo(e);

//清空原有数据
obsBBM.Clear();

List<BillingBookMerchant> listBBM = bll.bbmerList();

//将list<T>转化为ObservableCollection<T>
foreach (var item in listBBM)
{
obsBBM.Add(item);
}

this.lbMerchantList.ItemsSource = obsBBM;

}

/// <summary>
/// 添加新的记帐本商家
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
this.NavigationService.Navigate(new Uri("/BillingBookMerchantEdit.xaml", UriKind.Relative));
}

/// <summary>
/// 删除选中记帐本商家
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
var reult = MessageBox.Show("你确定要删除吗?", "提示信息",MessageBoxButton.OKCancel);

if (reult == MessageBoxResult.OK)
{
BillingBookMerchant delbbm = this.lbMerchantList.SelectedItem as BillingBookMerchant;
int MerchantId = delbbm.MerchantId;

int flag = bll.DelMerchant(MerchantId);

if (flag > 0)
{
MessageBox.Show("删除成功!", "提示信息", MessageBoxButton.OK);
obsBBM.Remove(delbbm);
return;
}
else
{
MessageBox.Show("删除失败!", "提示信息", MessageBoxButton.OK);
return;
}
}
else
{
return;
}
}

/// <summary>
/// 修改选择中的记帐本商家
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEdit_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.lbMerchantList.SelectedValue.ToString()))
{
int MerchantId = (this.lbMerchantList.SelectedItem as BillingBookMerchant).MerchantId;
this.NavigationService.Navigate(new Uri("/BillingBookMerchantEdit.xaml?MerchantId=" + MerchantId + "", UriKind.Relative));
}
else
{
MessageBox.Show("请选择要修改记帐本商家!", "提示", MessageBoxButton.OK);
return;
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐