none
How to reflect the data updates for a binding to a method invoked property? RRS feed

  • 问题

  • Hi,

    I met an issue these days when I was developing a program, hope someone here can help me.

     

     

    I have a Recipe class such as:

     

    public class Recipe

    {

    public int RecipeId{ get;set;}

    public string RecipeName {get;set;}

    public List<string> BelongedCategories

    {

    get

    {

    return RecipeService.GetRecipeCategories(this.RecipeId);

    }

    }

     

    //other properties...

    }

     

    and I used bindings to bind the BelongedCategories property to a TextBlock control as well as the RecipeId and RecipeName properties to other textblock controls in Window1, then I updated the BelongedCategories in another WPF window, now I need to refresh the BelongedCategories bound value displayed in Window1, as it is a method invoked property, how can I manually update the binding to reflect the latest data? Or, any way to reflect the data updates automatically?

     

    Currently I used a way to clear the binding on the TextBlock control first and rebind it again to make the data refreshed, but I am expecting a better way to do so.


    2011年7月17日 14:26

答案

全部回复

  • 你好,

    你可以通过INotifyPropertyChanged.PropertyChanged事件来代替重绑定的动作。

    下面是一个简单的例子。

      <StackPanel Name="Panel" Loaded="Panel_Loaded">
        <TextBox Text="{Binding Number1, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Text="{Binding Number2}"/>
      </StackPanel>
    

        private void Panel_Loaded(object sender, RoutedEventArgs e)
        {
          Panel.DataContext=new MyClass(){Number1=10};
        }
    
        public class MyClass : INotifyPropertyChanged
        {
          protected void OnNotifyPropertyChanged(string p)
          {
            if (PropertyChanged != null)
            {
              PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
          }
          public event PropertyChangedEventHandler PropertyChanged;
    
          int _Number1;
          public int Number1
          {
            get { return _Number1; }
            set
            {
              if (_Number1 != value)
              {
                _Number1 = value;
                OnNotifyPropertyChanged("Number1");
                OnNotifyPropertyChanged("Number2");
              }
            }
          }
    
          public int Number2
          {
            get
            {
              return Number1 * 2;
            }
          }
        }
    


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年7月20日 4:03
    版主
  • Yes, you're right, in the sample you provided we can use PropertyChanged event to replace the rebinding action as it can be automatically reflect the data changes in controls, but my situation is a little different from your sample, consider following code:

      <StackPanel Name="Panel" Loaded="Panel_Loaded">
        <TextBlock Text="{Binding PersonId}" Name="tbkPersonId"/>
        <TextBlock Text="{Binding PersonName}" Name="tbkPersonName"/>
      </StackPanel>


        private void Panel_Loaded(object sender, RoutedEventArgs e)
        {
          Panel.DataContext=PersonService.GetPersonById(1);
        }

        public class Person
        { 
            public int PersonId {get;set;}
            public string PersonName
            {
               get
               {
                    //This method retieves person name directly from the database.
                    //Here I just write for demo, althrough in actual code we won't
                    //retrieve person name from database independently.
                    PersonService.GetPersonNameById(this.PersonId);         
               }
            }
        }

    Then, the user edit the person name for this person in another window and save it successfully to database, at this time, how to make the TextBlock 'tbkPersonName' being refreshed automatically to reflect the data changes in database?

    2011年7月20日 9:24
  • 你好,

    如我们之前在英文论坛中讨论的,要解决这个问题,可以对数据存取与更新进行集中管理。

    解决问题的关键在于知道何时去更新数据。把更新通知给绑定并不困难。

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年7月22日 2:08
    版主