Silverlight / WPF - INotifyPropertyChanged in WPF C# Using Linq to sql

Asked By Rajuje Abbasi
17-Jan-12 04:44 PM
I try to us the  INotifyPropertyChanged  in WPF C# Using Linq to sql But i can't do this because i am using the Linq to sql Class and i search the internet for this help but i only see in whole the Net only that how to right the INotifyPropertyChanged   class as i write 


 class Student : INotifyPropertyChanged
    {
        private int StudentID;
        private string FirstName;
        private string  LastName;
        private char Gender;
        private int GPA;
        private string  MyImage;

        public int ID
        {
            get
            {
                return StudentID;
            }
            set
            {
                StudentID = value;
                OnPropertyChanged("ID");
            }
        }


        public string Name
        {
            get
            {
                return FirstName;
            }
            set
            {
                FirstName = value;
                OnPropertyChanged("Name");
            }


        }


        public double LastName
        {


            get
            {
                return LastName;
            }
            set
            {
                LastName = value;
                OnPropertyChanged("LastName");
            }
        }




        public double Gender
        {


            get
            {
                return m_Gender;
            }
            set
            {
                LastName = value;
                OnPropertyChanged("Gender");
            }
        }


        public double GPA
        {


            get
            {
                return m_GPA;
            }
            set
            {
                LastName = value;
                OnPropertyChanged("GPA");
            }
        }


        public double MyImage
        {


            get
            {
                return m_MyImage;
            }
            set
            {
                LastName = value;
                OnPropertyChanged("MyImage");
            }
        }




        #region INotifyPropertyChanged Members


        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
       
#endregion
    
Next step who to use in Mian window using linq to sql Clases i don't know or you can say that i even don't know that who to use ViewModel try like that as class ProductViewModel
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        private IList<Student> m_Products;
        public ProductViewModel()
        {

        }
        public IList<Product> Products
        {
            get
            {
                return m_Products;
            }
            set
            {
                m_Products = value;
            }
        }
        private ICommand mUpdater;
        public ICommand UpdateCommand
        {
            get
            {
                if (mUpdater == null)
                    mUpdater = new Updater();
                return mUpdater;
            }
            set
            {
                mUpdater = value;
            }
        }


        private class Updater : ICommand
        {
            #region ICommand Members


            public bool CanExecute(object parameter)
            {
                return true;
            }


            public event EventHandler CanExecuteChanged;


            public void Execute(object parameter)
            {


            }


            #endregion
        }

I know that i am wrong but how to use the INotifyPropertyChanged using linq to sql is my question my Student class is  
 
Class have above properties please tell me who to use INotifyPropertyChanged  Using linq to sql Class in wpf . I will be very thanks full to all those who will responding me please i a waiting for your replay please tell me in detail i am new in wpf using linq to sql class i try to update my image in which is in varBinary(MAX) type. Please pleasee


    
  [)ia6l0 iii replied to Rajuje Abbasi
17-Jan-12 09:57 PM
Wait a sec. Before we check what is wrong with INotifyPropertyChanged, the properties of Student class are not set the right way. Properties like Gender, GPA and MyImage have wrong set implmentations. They are setting the Lastname instead.

public double Gender
{...
           set
            {
                LastName = value;
                OnPropertyChanged("Gender");
            }
}

public double GPA
{...  
          set
            {
                LastName = value;
                OnPropertyChanged("GPA");
            }
}

public double MyImage
{
...
            set
            {
                LastName = value;
                OnPropertyChanged("MyImage");
            }
}
And on the MyImage property - how can you parse Image into a double value? That is wrong as well.

Please correct this first. And let us know what problems you are facing.
  D Company replied to Rajuje Abbasi
17-Jan-12 11:32 PM
Hello Rajuje,

Yes , you have used wroung casting in your class,have a look at your Student class properties.
 

1. Correct these below things

Gender returns Double, it should be either string.

2. Last Name is again double change it as string

3. Make Sure what GPA is , if it is like some numeric value than its ok, otherwise you have to change this also.

4. last but not least my Image is also having wroung data type change this also

Try fixing that first and see if your issue goes away.



Regards
D

  kalpana aparnathi replied to Rajuje Abbasi
18-Jan-12 03:11 AM
hi,

INotifyPropertyChanged

- You only need to implement this interface if you data class is going to change and you want those changes to propagate through to the UI.

- This interface is not required for LINQ to SQL to work

- This interface is not required to do read-once data binding

- If you’re going to be reading data from the data base and simply displaying it on the screen then you can, and should, leave this interface off as it just adds unnecessary clutter

public class Customer : INotifyPropertyChanged
{
}
  Define INotifyPropertyChanged Members,
 
  public event PropertyChangedEventHandler PropertyChanged;
 
  public void OnPropertyChanged(PropertyChangedEventArgs e)
  {
     if (PropertyChanged != null)
     {
     PropertyChanged(this, e);
     }
  }
 
  In property setter invoke OnPropertyChanged by passing property name like,
 
  private string _Name;
 
  public string Name
  {
    get
    {
     return _Name;
    }
    set
    {
    _Name = value;
    OnPropertyChanged(new PropertyChangedEventArgs("Name"));
    }
  }
 
  In MainPage.xaml.cs add an ObservableCollection of customer object as Dependency
  property inorder to make sure that UI is updating while we assigning that customer list
  to another list or object.If we are making it as a normal property UI will update only if
  we add new object to customerlist or any change occurs to the underlying properties.
 
  public ObservableCollection<Customer> CustomerList
  {
    get { return (ObservableCollection<Customer>)
  GetValue(CustomerListProperty); }
    set { SetValue(CustomerListProperty, value); }
  }
 
  // Using a DependencyProperty as the backing store for MyProperty.
  This enables animation, styling, binding, etc...
  public static readonly DependencyProperty CustomerListProperty =
  DependencyProperty.Register("CustomerList",
  typeof(ObservableCollection<Customer>), typeof(MainPage),
    new PropertyMetadata(new ObservableCollection<Customer>()));
 
  I also added a DependencyProperty FirstName in MainPage.xaml.cs just to show the binding of a simple DependencyProperty.
 
  public string FirstName
  {
    get { return (string)GetValue(FirstNameProperty); }
    set { SetValue(FirstNameProperty, value); }
  }
 
  // Using a DependencyProperty as the backing store for MyProperty.
  This enables animation, styling, binding, etc...
  public static readonly DependencyProperty FirstNameProperty =
    DependencyProperty.Register("FirstName", typeof(string), typeof(MainPage),
    new PropertyMetadata(string.Empty));
 
  In MainPage.XAML add a datagrid and textbox and bind it to the ObservableCollection and DependencyProperty respectively.
 
  <data:DataGrid AutoGenerateColumns="True"
   Width="400"
   Height="300"
   ItemsSource="{Binding ElementName=TestUC,
                  Path=CustomerList}"/>
 
  <TextBox x:Name="NameTextBox"
  Text="{Binding ElementName=TestUC, Path=FirstName, Mode=TwoWay}"
  Width="100"
  Height="25"
  Margin="0,10,0,10" />

  Sree K replied to Rajuje Abbasi
18-Jan-12 03:47 AM

Look at your Student class' properties. Their 'setters' are all setting LastName to value.

Try fixing that first and see if your issue goes away.

  Rajuje Abbasi replied to kalpana aparnathi
19-Jan-12 08:21 PM
Problem is that when i try to use my student table as you use the costumer table as 
public ObservableCollection<Customer> CustomerList
  {
  get { return (ObservableCollection<Customer>)
  GetValue(CustomerListProperty); }
  set { SetValue(CustomerListProperty, value); }
  }
then my student table not appear or you can say that not functioning just not studnet table but all my others table as will . I short 
public ObservableCollection<Student> CustomerList at here my student table is not functioning
  {
  get { return (ObservableCollection<Student>)
  GetValue(CustomerListProperty); }
  set { SetValue(CustomerListProperty, value); }
  }
Create New Account
help
How to consume INotifyPropertyChanged events .NET Framework I have a class that implements INotifyPropertyChanged. Parts of a UI are bound to it, and they work as expected. However, how so that I do not need to create new event handlers? C# Discussions System.ComponentModel.PropertyChangedEventHandler (1) System.ComponentModel.PropertyChangedEventArgs (1) PropertyChangedEventHandler (1) PropertyChangedEventArgs (1) INotifyPropertyChanged (1) NotImplementedException (1) eed What exactly are you trying to accomplish? Wouldn't the setter suffice? When a property changes, the PropertyChanged PropertyChangedEventHandler is called. I'd like to *hook* into that in the same way a binding
does not implement InotifyPropertyChanged interface here is the code:- public class QueryField : ICloneable , INotifyPropertyChanged { region * * Icloneable region * * InotifyPropertyChanged } it does not implemented it gives error and if implement explictly it creates another stub NotifyPropertyChanged event in our class. Here is how it should look: Public Event PropertyChanged As PropertyChangedEventHandler _ Implements INotifyPropertyChanged.PropertyChanged Private Sub NotifyPropertyChanged(ByVal info As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info)) End Sub and for each property's set method call the NofityPropertyChanged("propertyname"). Here
framework so that changes to a property would notify any interested listeners? Thanks. C# Discussions PropertyChangedEventHandler (1) PropertyChangedEventArgs (1) INotifyPropertyChanged (1) EventArgs (1) PropertyChanged (1) Show (1) InitializeComponent (1) Click (1) On Wed, 19 Nov this issue. To notify clients that a property value has changed, you can implement the INotifyPropertyChanged interface on your classes. For example, Hello Gerhard, Thank you for using Microsoft Managed Newsgroup this issue. To notify clients that a property value has changed, you can implement the INotifyPropertyChanged interface on your classes. For example, = = = = = = = = = = = = = = Code Sample = = = = = = = = = = = = = = = = = class Customer : INotifyPropertyChanged { private int id; private string name; public Customer(int id, string name) { this.id = id NotifyPropertyChanged("ID"); } } public string Name { get { return name; } set { this.name = value; NotifyPropertyChanged("Name"); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged ! = null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } #endregion
control on Form 2. Regards Charles Mifsud Shireburn Software Ltd. .NET Discussions VB.NET (1) PropertyChangedEventHandler (1) INotifyPropertyChanged (1) PropertyChangedEventArgs (1) EventArgs (1) EventHandler (1) MenuItem (1) PropertyChanged (1) What do you mean by linking the bound buttons notified when the data source is changed, we need to implement the INotifyPropertyChanged interface on the Class1. The following is a sample. using System.ComponentModel; class Class1:INotifyPropertyChanged { public bool enabled = true; public bool Enabled { get { return enabled; } set { enabled = value; if (PropertyChanged null) PropertyChanged(this, new PropertyChangedEventArgs("Enabled")); } } public static void Method1(object sender, EventArgs e) { MessageBox.Show("calling method1"); } public event PropertyChangedEventHandler PropertyChanged; } private void Form1_Load(object sender, EventArgs e) { this.button1.Click + = new EventHandler(Class1.Method1