Windows Phone 学习 LINQ在wp上的使用

来源:互联网 发布:地板革淘宝网 编辑:程序博客网 时间:2024/06/06 03:56
  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">            <ListBox Name="MyContactList">                <ListBox.ItemContainerStyle>                    <Style TargetType="ListBoxItem">                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>                        <Setter Property="Margin" Value="2,6,2,6"/>                    </Style>                </ListBox.ItemContainerStyle>                <ListBox.ItemTemplate>                    <DataTemplate>                        <Grid>                            <Grid.ColumnDefinitions>                                <ColumnDefinition Width="*"/>                                <ColumnDefinition Width="auto"/>                            </Grid.ColumnDefinitions>                            <StackPanel Grid.Column="0">                                <TextBlock FontSize="32" Text="{Binding Name}"/>                                <StackPanel Orientation="Horizontal">                                    <TextBlock Text="电话:"/>                                    <TextBlock Text="{Binding PhoneNo}"/>                                </StackPanel>                                <StackPanel Orientation="Horizontal">                                    <TextBlock Text="电邮:"/>                                    <TextBlock Text="{Binding Email}"/>                                </StackPanel>                            </StackPanel>                            <StackPanel Grid.Column="1">                                <HyperlinkButton Content="删除" Tag="{Binding ID}" Click="OnDelete" Margin="0,25,7,0"/>                            </StackPanel>                        </Grid>                    </DataTemplate>                </ListBox.ItemTemplate>            </ListBox>        </Grid>
  public MainPage()        {            InitializeComponent();        }        // 删除        private void OnDelete(object sender, RoutedEventArgs e)        {            if (MessageBox.Show("确定删除吗?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)            {                HyperlinkButton hb = e.OriginalSource as HyperlinkButton;                if (hb != null)                {                    int cID = (int)hb.Tag;                    using (MyDataEntities.MyDataContext dc = new MyDataEntities.MyDataContext())                    {                        MyDataEntities.Contacts ct = dc.Contacts.FirstOrDefault(c => c.ID == cID);                        if (ct != null)                        {                            dc.Contacts.DeleteOnSubmit(ct);                            dc.SubmitChanges();                        }                    }                }                BindData();            }        }        // 以便在进入页面绑定数据        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)        {            base.OnNavigatedTo(e);            BindData();        }        /// <summary>        /// 将数据绑定到ListBox        /// </summary>        private void BindData()        {            using (MyDataEntities.MyDataContext dc = new MyDataEntities.MyDataContext())            {                var res =                    from c in dc.Contacts                    select c;                this.MyContactList.ItemsSource = res.ToList();            }        }        // 导航到新增联系人页面        private void OnNewContact(object sender, EventArgs e)        {            NavigationService.Navigate(new Uri("/NewContact.xaml", UriKind.Relative));        }

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >            <Grid.ColumnDefinitions>                <ColumnDefinition Width="Auto" />                <ColumnDefinition />            </Grid.ColumnDefinitions>            <Grid.RowDefinitions>                <RowDefinition Height="Auto" />                <RowDefinition Height="Auto" />                <RowDefinition Height="Auto" />            </Grid.RowDefinitions>            <TextBlock Text="姓名:" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" />            <TextBox Name="txtName" Grid.Column="1" Grid.Row="0" />            <TextBlock Grid.Row="1" Grid.Column="0" Text="电话:" VerticalAlignment="Center"/>            <TextBox Name="txtPhoneNo" Grid.Column="1" Grid.Row="1">                <TextBox.InputScope>                    <InputScope>                        <InputScopeName NameValue="NameOrPhoneNumber"/>                    </InputScope>                </TextBox.InputScope>            </TextBox>            <TextBlock Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" Text="E-mail:"/>            <TextBox Name="txtEmail" Grid.Row="2" Grid.Column="1">                <TextBox.InputScope>                    <InputScope>                        <InputScopeName NameValue="EmailNameOrAddress"/>                    </InputScope>                </TextBox.InputScope>            </TextBox>        </Grid>    </Grid>

  public NewContact()        {            InitializeComponent();        }        private void OnOK(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(txtName.Text))            {                MessageBox.Show("联系人姓名必须输入。"); return;            }            using (MyDataEntities.MyDataContext dc = new MyDataEntities.MyDataContext())            {                MyDataEntities.Contacts ct = dc.Contacts.FirstOrDefault(c => c.Name == txtName.Text);                if (ct != null)                {                    MessageBox.Show("该联系人姓名已存在。"); return;                }                ct = new MyDataEntities.Contacts()                {                    Name = txtName.Text,                    PhoneNo = txtPhoneNo.Text,                    Email = txtEmail.Text                };                dc.Contacts.InsertOnSubmit(ct);                dc.SubmitChanges();            }            if (NavigationService.CanGoBack)            {                NavigationService.GoBack();            }        }        private void OnCancel(object sender, EventArgs e)        {            if (NavigationService.CanGoBack)            {                NavigationService.GoBack();            }        }

  public class Contacts : INotifyPropertyChanging, INotifyPropertyChanged    {        #region 公共属性        int _ID;        [Column(DbType = "INT IDENTITY NOT NULL", CanBeNull = false, IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]        public int ID        {            get            {                return this._ID;            }            set            {                if (this._ID != value)                {                    OnPropertyChanging("ID");                    this._ID = value;                    OnPropertyChanged("ID");                }            }        }        string _Name;        [Column]        public string Name        {            get            {                return this._Name;            }            set            {                if (this._Name != value)                {                    OnPropertyChanging("Name");                    this._Name = value;                    OnPropertyChanged("Name");                }            }        }        string _PhoneNo;        [Column]        public string PhoneNo        {            get            {                return this._PhoneNo;            }            set            {                if (this._PhoneNo != value)                {                    OnPropertyChanging("PhoneNo");                    this._PhoneNo = value;                    OnPropertyChanged("PhoneNo");                }            }        }        string _Email;        [Column]        public string Email        {            get            {                return this._Email;            }            set            {                if (this._Email != value)                {                    OnPropertyChanging("Email");                    this._Email = value;                    OnPropertyChanged("Email");                }            }        }        #endregion        #region INotifyPropertyChanging接口和INotifyPropertyChanged的成员        public event PropertyChangingEventHandler PropertyChanging;        public event PropertyChangedEventHandler PropertyChanged;        #endregion        #region 引发事件的方法        protected void OnPropertyChanging(string propertyName)        {            if (this.PropertyChanging != null)            {                this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));            }        }        protected void OnPropertyChanged(string propertyName)        {            if (this.PropertyChanged != null)            {                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }        #endregion    }    /// <summary>    /// 用于操作数据库的上下文    /// </summary>    public class MyDataContext : DataContext    {        /// <summary>        /// 连接字符串        /// </summary>        public const string CONN_STR = "data source='isostore:/db.sdf';password='123456'";        /// <summary>        /// 公共构造函数        /// </summary>        public MyDataContext() : base(CONN_STR) { }        /// <summary>        /// 公共成员        /// </summary>        public Table<Contacts> Contacts;    }


0 0