2016-01-25 72 views
0

我試圖控制的可視性綁定到一個全局變量:UWP窗口10能見度綁定到一個全局變量

我有能見度轉換器:

public class BooleanToVisibilityConverter : IValueConverter 
{ 
    public bool IsReversed { get; set; } 

    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     return value is Visibility && (Visibility)value == Visibility.Visible; 
    } 


} 

我還創建了一個類,自DependencyObject繼承:

public class Verified : DependencyObject 
{ 
    public static readonly DependencyProperty VerifiedUserNameProperty = DependencyProperty.Register(
     "VerifiedUserName", 
     typeof(string), 
     typeof(Verified), 
     new PropertyMetadata(string.Empty) 
     ); 

    public string VerifiedUserName 
    { 
     get { return (string)GetValue(VerifiedUserNameProperty); } 
     set { SetValue(VerifiedUserNameProperty, value); } 
    } 

    public static readonly DependencyProperty IsVerifiedProperty = DependencyProperty.Register(
     "IsVerified", 
     typeof(bool), 
     typeof(Verified), 
     new PropertyMetadata(false, new PropertyChangedCallback(IsVerifiedChanged)) 
    ); 

    public bool IsVerified 
    { 
     get { return (bool)GetValue(IsVerifiedProperty); } 
     set { SetValue(IsVerifiedProperty, value); } 
    } 

    private static void IsVerifiedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     Verified container = d as Verified; 
     bool latestIsVerifiedValue = (bool)e.NewValue; 
     if (latestIsVerifiedValue == true) 
     { 
      //To Do 
     } 
    } 
} 
在我的App.xaml

我創建變量:

public static Verified myValidator = new Verified(); 

public static BooleanToVisibilityConverter myConverter = new BooleanToVisibilityConverter(); 

在一個簡單的頁面中,我添加了一個文本框,並且我想將它的可見性綁定到myValidator變量。

爲了實現這個目標,我在代碼中創建綁定:

Binding myBinding = new Binding(); 
myBinding.Source = App.myValidator; 
myBinding.Path = new PropertyPath("IsVerified"); 
myBinding.Mode = BindingMode.OneWay; 
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
myBinding.Converter = App.myConverter; 
BindingOperations.SetBinding(txtName2, VisibilityProperty, myBinding); 

在App.xaml中:我設置我的驗證的價值是這樣的:

App.myValidator.IsVerified = false; 

當我導航到簡單頁面的文本框不可見(OK)。

爲了測試行爲,我在屏幕上添加了一個按鈕,並添加了代碼以更改myValidator.isVerified的值。

當我更改我的App.myValidator.IsVerified的值時,文本框的可見性不會改變。我該如何解決這個問題?

+0

嘗試在轉換器設置斷點,看看如果它實際上受到打擊。我懷疑它不是。 –

+0

它第一次到達轉換器。我更改IsVerified的值時驗證器的propertyChange事件始終正確執行。但在第一次轉換器沒有再被擊中之後。 – user1282441

+0

不知道這是否會這樣做,但也嘗試從'INotifyPropertyChanged'繼承並實現它,然後看它是否有效。 –

回答

3

綁定系統使用反射來查找屬性,它不尋找「字段」。

當你聲明

public static Verified myValidator = new Verified(); 

你聲明一個領域,而不是財產。

在App.cs更改myValidator的屬性將解決這個問題,下面,

public static Verified myValidator { get; set; } = new Verified(); 

更新! 它工作的原因是因爲我不小心聲明中MainPage.cs經過驗證的資源,

public sealed partial class MainPage : Page 
{ 
    public Verified myValidator1 {get; set;} // the property name does not matter 

    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 
} 

不知道爲什麼它的作品,我仍然在尋找到它。

+0

我這樣做,它沒有工作。我想我以前嘗試過,我嘗試過多種組合,但沒有成功。在某個時候,它正在工作,當時我正在使用資源以聲明方式進行綁定。但我無法找到一種方法來處理全局變量。 – user1282441

+0

我意識到我在MainPage.cs中聲明瞭一個Verified實例(public Verified myVerified {get; set;}),其中聲明瞭txtName2。對我來說,添加這行代碼使綁定起作用似乎很奇怪。 – Jackie

1

基本問題是您的財產不發出更改的事件,所以您的綁定甚至不知道該屬性已更改,更不用說它應該調用您的轉換方法。實現INotifyPropertyChanged應該在屬性更改時通知它,然後它應該調用轉換器並執行您想要的操作。

0

首先,我可以在我身邊重現這個問題。 要驗證根本原因,我註冊了已驗證。IsVerifiedProperty的的PropertyChanged回調:

private long token; 

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
      token = App.myValidator.RegisterPropertyChangedCallback(Verified.IsVerifiedProperty, ChangedCallback); 
      base.OnNavigatedTo(e); 
} 

private void ChangedCallback(DependencyObject sender, DependencyProperty dp) 
{ 
      if (dp == Verified.IsVerifiedProperty) 
      { 
       System.Diagnostics.Debug.WriteLine("ChangedCallback" + sender.GetValue(dp)); 
       System.Diagnostics.Debug.WriteLine("ChangedCallback" + txtName2.Visibility); 
      } 
} 

protected override void OnNavigatedFrom(NavigationEventArgs e) 
{ 
      App.myValidator.UnregisterPropertyChangedCallback(Verified.IsVerifiedProperty, token); 
      base.OnNavigatedFrom(e); 
} 

如預期的PropertyChanged無法被觸發,因此該解決方案是實現INotifyPropertyChanged的用於驗證類接口:

public class Verified : DependencyObject, INotifyPropertyChanged 
{ 
     public static readonly DependencyProperty IsVerifiedProperty = DependencyProperty.Register(
      "IsVerified", 
      typeof(bool), 
      typeof(Verified), 
      new PropertyMetadata(false, new PropertyChangedCallback(IsVerifiedChanged)) 
     ); 

     public bool IsVerified 
     { 
      get { return (bool)GetValue(IsVerifiedProperty); } 
      set { SetValue(IsVerifiedProperty, value); 
       if (PropertyChanged != null) 
        PropertyChanged(this, new PropertyChangedEventArgs("IsVerified")); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private static void IsVerifiedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      Verified container = d as Verified; 
      bool latestIsVerifiedValue = (bool)e.NewValue; 
      if (latestIsVerifiedValue == true) 
      { 
       //To Do 
      } 
     } 
}