为什么我的XAML没有响应变量的变化

时间:2015-05-17 08:08:13

标签: c# xaml windows-phone-8.1

这可能是一个非常简单的原因,为什么这不起作用,但我已经尝试了一切。我有一个TextBlock,文本绑定到一个变量,变量发生了变化,但Text没有:

 <TextBlock x:Name="modeLabel" Style="{StaticResource IndiTextBlock}"  Height="23" TextWrapping="Wrap" Grid.Row="0" Text="{Binding ModeLabelText}" Margin="35,22,58,0"/>

控制文本值的代码位于viewmodel中:

public string ModeLabelText { get { return _modeLabeltext; } }
public ComboBoxItem SelectedMode { get { return _selectedMode; }
set
{
    if (_selectedMode == value) return;
    _selectedMode = value;
    ToggleMode(null);
    EvaluateScenario(null);
}

private void ToggleMode(object parameter)
{
    if (_isBasicCalculation)
    {
        _modeLabeltext = "Target profit";
        _isBasicCalculation = false;
    }
    else
    {
        _modeLabeltext = "Total to invest";
        _isBasicCalculation = true;
    }
}

2 个答案:

答案 0 :(得分:2)

您的班级必须实施INotifyPropertyChanged界面,并且在更改变量时,您应该触发事件

public class Model : INotifyPropertyChanged
{
    public event EventHandler PropertyChanged; // event from INotifyPropertyChanged

    protected void RaisePropertyChanged(string propertyName)
    {
        var local = PropertyChanged;
        if (local != null)
        {
            local.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public void ToggleMode()
    {
        // ... your code ...
        RaisePropertyChanged("ModelLabelText");
    }
}

答案 1 :(得分:0)

谢谢Nguyen Kien

    private void ToggleMode(object parameter)
    {
        if (_isBasicCalculation)
        {
            _modeLabeltext = "Target profit";
            OnPropertyChanged("ModeLabelText");
            _isBasicCalculation = false;

        }
        else
        {
            _modeLabeltext = "Total to invest";
            OnPropertyChanged("ModeLabelText");
            _isBasicCalculation = true;
        }

    }
相关问题