2017-10-04 71 views
0

我有一個類「A」和類「VisibilityStateProvider」每當任何關鍵的類A改變,那時我調用VisibilityStateProvider的NotifyVisibilityChanged但GetVisibilityValue(key)方法永遠不會調用。可見性不會在UI上得到更新。UWP綁定不工作

感謝您的幫助!

 public class A: IDictionary<string, object>, INotifyPropertyChanged 

       { 
        public VisibilityStateProvider VisibilityProvider { get; set; } 

        public A() 
        { 
         VisibilityProvider = new VisibilityStateProvider(this); 
        } 


       } 
      public class VisibilityStateProvider : IPropertyStateProvider 
       { 
        private A A; 

        public VisibilityStateProvider(A a) 
        { 
         A= a; 
        } 
       public bool this[string key] 
        { 
        get 
        { 
         return GetVisibilityValue(key); 
        } 
       } 
      private bool GetVisibilityValue(string key) 
        { 
        //returns true or false on basis of some condition. 
        } 
     public void NotifyVisibilityChanged(string key) 
      { 
       var a= $"Item[{key}]"; 

       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(a)); 
      } 
      } 
      public interface IPropertyStateProvider : INotifyPropertyChanged 
       { 
        bool this[string key] { get;} 
       } 


      //xaml 
      <TextBox Text="{Binding A[mf_number1], Mode=TwoWay}" Visibility="{Binding A.VisibilityProvider[mf_number1],Converter={StaticResource BoolToVisibilityConverter}}" ></TextBox > 

回答

0

我還沒有檢查完整的代碼,所以在這裏我將分享一個你需要知道的事情作爲開始。在你的代碼中,GetVisibilityValue(key)永遠不會被調用,因爲你沒有以正確的方式調用它。試試下面的代碼作爲測試:

public A() 
    { 
     VisibilityProvider = new VisibilityStateProvider(this); 
     var something = VisibilityProvider["test"]; 
    } 

這裏只有當你調用類的第二行,那麼你下面的代碼可以被稱爲:

  get 
       { 
        return GetVisibilityValue(key); 
       } 
+0

做這樣GetVisibilityValue方法被調用,但我希望通過xaml綁定來調用它 –