使用INotifyPropertyChanged绑定到属性的XAML仅适用于App启动

时间:2014-04-15 22:49:28

标签: c# xaml windows-phone-8 data-binding inotifypropertychanged

我在数据模型中有以下代码,我在XAML中设置了绑定。问题是“初始”值已正确加载到XAML中,但没有反映出任何变化。这是我第一次使用INotify,所以我有点失落......

public class MainViewModel : INotifyPropertyChanged
{
    public MainViewModel()
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
        if (propertyName=="BasicInfoText")
        {
            AdvancedInfoText = "Basic info updated";
        }
        if (propertyName=="AdvancedInfoText")
        {
            //do nothing
        }
    }

    //text for the basic info 
    public String BasicInfoText
    {
        get { return _basicInfoText; }
        set
        {
            if (_basicInfoText != value)
            {
                _basicInfoText = value;
                NotifyPropertyChanged("BasicInfoText");
            }
        }
    }
    private String _basicInfoText = "Initial text";
    //text for the advanced info 
    public String AdvancedInfoText
    {
        get { return _advancedInfoText; }
        set
        {
            if (_advancedInfoText != value)
            {
                _advancedInfoText = value;
                NotifyPropertyChanged("AdvancedInfoText");
            }
        }
    }
    private String _advancedInfoText = "Initial Advance Text";

我还试图将private _basicInfoText和_advancedInfoText更改为public,以确保通知更改,但没有运气。请问任何建议?

UPDATE>

所以我试图注册事件,但仍然没有工作。代码现在看起来像这样:

public class MainViewModel : INotifyPropertyChanged
{
    public MainViewModel()
    {

                this.PropertyChanged += MyViewModel_PropertyChanged;
    }
    //***********************************
    //catching events
    void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "BasicSelected":
                {// Do something
                    BasicInfoText = "basic text";
                    break;
                }
            case "WideSelected":
                {// Do something
                    BasicInfoText = "wide text";
                    break;
                }
            case "NarrowSelected":
                {// Do something
                    BasicInfoText = "narrow text";
                    break;
                }
            case "ArtificalSelected":
                {// Do something
                    BasicInfoText = "artificial text";
                    break;
                }
        }
    }


    //***********************************
    //register handlers
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }


    protected void OnPropertyChanged(String propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    //properties for the calculation and behavior
    public Boolean BasicSelected
    {
        get { return _basicSelected; }
        set {
              if (_basicSelected != value)
              {
                _basicSelected = value;
                OnPropertyChanged("BasicSelected");
             }
            }
    }
    public Boolean _basicSelected = true;
        //properties for the calculation and behavior
    public Boolean WideSelected
    {
        get { return _wideSelected; }
        set
        {
            if (_wideSelected != value)
            {
                _wideSelected = value;
                OnPropertyChanged("WideSelected");
            }
        }
    }
    public Boolean _wideSelected = false;
    //properties for the calculation and behavior
    public Boolean NarrowSelected
    {
        get { return _narrowSelected; }
        set
        {
            if (_narrowSelected != value)
            {
                _narrowSelected = value;
                OnPropertyChanged("NarrowSelected");
            }
        }
    }
    public Boolean _narrowSelected = false;
    //properties for the behavior test
    public Boolean ArtificalSelected
    {
        get { return _artificalSelected; }
        set
        {
            if (_artificalSelected != value)
            {
                _artificalSelected = value;
                OnPropertyChanged("ArtificalSelected");
            }
        }
    }
    public Boolean _artificalSelected = false;
    //text for the basic info 
    public String BasicInfoText
    {
        get { return _basicInfoText; }
        set
        {
            if (_basicInfoText != value)
            {
                _basicInfoText = value;
                OnPropertyChanged("BasicInfoText");
            }
        }
    }
    public String _basicInfoText = "Initial text 2";
    //text for the advanced info 
    public String AdvancedInfoText
    {
        get { return _advancedInfoText; }
        set
        {
            if (_advancedInfoText != value)
            {
                _advancedInfoText = value;
                OnPropertyChanged("AdvancedInfoText");
            }
        }
    }
    public String _advancedInfoText = "Initial Advance Text 2";

XAML看起来像这样:

`     

       <phone:Panorama>
        <phone:Panorama.Title>
            <TextBlock Height="120" Text="Test visu" FontSize="110"/>
        </phone:Panorama.Title>
        <phone:PanoramaItem Header="Test selection" FontSize="22">
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="70"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="5"/>
                    <RowDefinition Height="80"/>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="80"/>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="49*"/>
                    <ColumnDefinition Width="2*"/>
                    <ColumnDefinition Width="49*"/>
                </Grid.ColumnDefinitions>

                <TextBlock
         Name="InfoBasic"
                     Text="{Binding BasicInfoText}" 
                     Grid.Row="0" 
                     Grid.ColumnSpan="3"
                    HorizontalAlignment="Left"
        VerticalAlignment="Center"/>`

0 个答案:

没有答案