2012-01-25 262 views
0

我有一個用戶控件,它有問題綁定到IsEnabled的依賴項屬性。我也嘗試手動設置IsEnabled =「false」,這也似乎不起作用。不依賴於UserControl的依賴屬性

下面是代碼:

public partial class News : UserControl 
{ 
    public static readonly DependencyProperty IsAuthenticatedProperty = 
    DependencyProperty.Register(
    "IsAuthenticated", 
    typeof(bool), 
    typeof(News), 
    new FrameworkPropertyMetadata(
    new PropertyChangedCallback(ChangeAuth))); 

    public bool IsAuthenticated 
    { 
     get 
     { 
      return (bool) GetValue(IsAuthenticatedProperty); 
     } 
     set 
     { 
      SetValue(IsAuthenticatedProperty, value); 
     } 
    } 

    private static void ChangeAuth(DependencyObject source, DependencyPropertyChangedEventArgs e) 
    { 
     if (e.NewValue is bool == false) 
     { 
      (source as News).UpdateAuth(false); 
     } 
     else 
     { 
      (source as News).UpdateAuth(true); 
     } 
    } 

    private void UpdateAuth(bool value) 
    { 
     IsAuthenticated = value; 
    } 


    public News() 
    { 
     IsAuthenticated = false; 
     this.IsEnabled = false; 
     InitializeComponent(); 
    } 

什麼想法?提前致謝

回答

1

由於您沒有在XAML中顯示綁定,所以很難確定,但是,默認情況下,綁定將在DataContext中設置的任何對象上查找綁定屬性。我懷疑這是問題...

如果這個假設是正確的,類似的solution is presented over here ...