简体   繁体   中英

Data Binding doesn't update a value

I am playing around with data binding, i create a user control (view) like this:

<UserControl>
<Grid x:Name="LayoutRoot" 
      Background="{StaticResource PhoneChromeBrush}" 
      DataContext="{Binding Source={StaticResource ViewModelSampleDataSource}}"
      >
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <TextBox Text="{Binding Model.Var1, Mode=TwoWay}" InputScope="Number" Grid.Row="0" FontSize="90"/>
    <TextBlock Text="{Binding Model.Var2}" Grid.Row="1" FontSize="90" />
</Grid>

I have this class Model

 public class ModelSample:INotifyPropertyChanged
{
    public ModelSample()
    {

    }

    private double var1;
    public double Var1
    {
        get { return var1; }
        set 
        { 
            var1 = value;
            OnPropertyChanged("var"); 
        }
    }

    private double var2;
    public double Var2
    {
        get { return var2; }
        set 
        { 
            var2 = value; 
            OnPropertyChanged("var2"); 
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }
}

and this is the ViewModel

public class ViewModelSample
{
    private static ModelSample model=new ModelSample();
    public static ModelSample Model
    {
        get { return model; }
        set { model = value; }
    }

}

My problem is : when i change the value of var1 (using the textbox) i want to update the value of var2, how can i do it? thanks

The string values you use for OnPropertyChanged must match the property names instead of the field names. After fixing simply changing Var2 from inside Var1's setter should do the trick.

Change this

    private double var1;
public double Var1
{
    get { return var1; }
    set 
    { 
        var1 = value;
        OnPropertyChanged("var"); 
    }
}

private double var2;
public double Var2
{
    get { return var2; }
    set 
    { 
        var2 = value; 
        OnPropertyChanged("var2"); 
    }
}

to this

    private double var1;
public double Var1
{
    get { return var1; }
    set 
    { 
        var1 = value;
        OnPropertyChanged("Var"); 
    }
}

private double var2;
public double Var2
{
    get { return var2; }
    set 
    { 
        var2 = value; 
        OnPropertyChanged("Var2"); 
    }
}

Raising property changed must be case sensitive on its property name.

Try this:

private double var1;
public double Var1
{
    get { return var1; }
    set 
    { 
        var1 = value;
        OnPropertyChanged("Var1"); 
    }
}

private double var2;
public double Var2
{
    get { return var2; }
    set 
    { 
        var2 = value; 
        OnPropertyChanged("Var2"); 
    }
}

Note that the OnPropertyChanged string has to match the Property's name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM