Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would INotifyPropertyChanged only fire when at least TWO characters are changed?
    primarykey
    data
    text
    <p>I finally got a Silverlight MVVM example to work so that when I change the values of first name and last name text boxes, the full names change automatically.</p> <p>However, and strangely, my model which inherits from <strong>INotifyPropertyChanged is only notified if I change</strong> <strong><em>at least 2 characters</em></strong> of either the first or last name. </p> <ul> <li>if I change "Smith" to "Smith1", then <strong>no event is fired</strong></li> <li>if I change "Smith" to "Smith12" then the event is fired, as expected</li> </ul> <p>Has anyone run in to this before in Silverlight/XAML/INotifyPropertyChanged? What could it be? <strong>Is there a setting somewhere that indicates how much of a textbox needs to change before it notifies itself as "changed"?</strong></p> <p>Here are the main parts of the code I'm using:</p> <p><strong>Customer.cs:</strong></p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; namespace TestMvvm345.Model { public class Customer : INotifyPropertyChanged { public int ID { get; set; } public int NumberOfContracts { get; set; } private string firstName; private string lastName; public string FirstName { get { return firstName; } set { firstName = value; RaisePropertyChanged("FirstName"); RaisePropertyChanged("FullName"); } } public string LastName { get { return lastName; } set { lastName = value; RaisePropertyChanged("LastName"); RaisePropertyChanged("FullName"); } } public string FullName { get { return firstName + " " + lastName; } } #region INotify public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } #endregion } } </code></pre> <p><strong>CustomerHeaderView.xaml:</strong></p> <pre><code>&lt;UserControl x:Class="TestMvvm345.Views.CustomerHeaderView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"&gt; &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;StackPanel HorizontalAlignment="Left"&gt; &lt;ItemsControl ItemsSource="{Binding}"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBox x:Name="FirstName" Text="{Binding Path=FirstName, Mode=TwoWay}" Width="150" Margin="3 5 3 5"/&gt; &lt;TextBox x:Name="LastName" Text="{Binding Path=LastName, Mode=TwoWay}" Width="150" Margin="0 5 3 5"/&gt; &lt;TextBlock x:Name="FullName" Text="{Binding Path=FullName, Mode=TwoWay}" Margin="0 5 3 5"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p><strong>CustomerViewModel.cs:</strong></p> <pre><code>using System.ComponentModel; using System.Collections.ObjectModel; using TestMvvm345.Model; namespace TestMvvm345 { public class CustomerViewModel { public ObservableCollection&lt;Customer&gt; Customers { get; set; } public void LoadCustomers() { ObservableCollection&lt;Customer&gt; customers = new ObservableCollection&lt;Customer&gt;(); //this is where you would actually call your service customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 }); customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 22 }); customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 33 }); customers.Add(new Customer { FirstName = "Robert", LastName = "Smith", NumberOfContracts = 2 }); customers.Add(new Customer { FirstName = "Hank", LastName = "Jobs", NumberOfContracts = 5 }); Customers = customers; } } } </code></pre> <p><strong>MainPage.xaml.cs:</strong></p> <pre><code>void MainPage_Loaded(object sender, RoutedEventArgs e) { CustomerViewModel customerViewModel = new CustomerViewModel(); customerViewModel.LoadCustomers(); CustomerHeaderView.DataContext = customerViewModel.Customers; } </code></pre> <h1>UPDATE:</h1> <p>I remade this project in WPF and it works fine. Perhaps it's a Silverlight 3 issue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload