2014-06-23 70 views
0

我不明白爲什麼我在運行應用程序時遇到此異常。這是由依賴項屬性引起的異常嗎?

我在MainWindow類中創建了一個依賴項屬性,並且在這個屬性中調用了ImagesGrid類。

我沒有對xaml進行任何更改。我是否也應該在那裏綁定一些東西?

這對於依賴propoerty代碼:

public Visibility ButtonVisible 
     { 
      get { return (Visibility)GetValue(ButtonVisibleProperty); } 
      set { SetValue(ButtonVisibleProperty, value); } 
     } 

    public static readonly DependencyProperty ButtonVisibleProperty = 
     DependencyProperty.Register("ButtonVisible", typeof(Visibility), typeof(MainWindow), new PropertyMetadata(false)); 

這是我呼籲物業

if (selectedModel is WineGroupModel) 
     { 
      MainWindow winesWindow = new MainWindow(); //mainwindow 
      winesWindow.ButtonVisible = System.Windows.Visibility.Hidden; 
      //some code 
     } 

這是構造主窗口

public MainWindow() 
    { 
     this.InitializeComponent(); 

     this.DataContext = this; 

     ImagesDir = @".\GalleryImages"; 

    } 

這是按鈕XAML代碼:

<k:KinectCircleButton Style="{StaticResource BackButtonStyle}" Foreground="#511C74" Name="BacKinectCircleButton" Label=""></k:KinectCircleButton> 

這是圖像:

http://i60.tinypic.com/5zqt5.png

+0

看看http://i59.tinypic.com/15qp53c.png – user3137167

+0

@ user3137167,不要作爲圖像發佈。許多企業代理阻止像tinypic.com這樣的網站。發佈錯誤 –

+0

好的文字。我會這樣做 – user3137167

回答

1

您已設置該屬性的錯誤的默認值,因爲它的類型爲Visibility,不bool

更改聲明

public static readonly DependencyProperty ButtonVisibleProperty = 
    DependencyProperty.Register(
     "ButtonVisible", typeof(Visibility), typeof(MainWindow), 
     new PropertyMetadata(Visibility.Collapsed)); 

或離開了完全的屬性的元數據:

public static readonly DependencyProperty ButtonVisibleProperty = 
    DependencyProperty.Register(
     "ButtonVisible", typeof(Visibility), typeof(MainWindow)); 

您將按鈕的Visibility屬性綁定到這樣的特性:

<k:KinectCircleButton ... Visibility="{Binding Path=ButtonVisible, 
    RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" /> 
+0

是的,錯誤已經消失了.. thankx,Clemeans。爲了隱藏按鈕,我應該將它綁定到我創建的這個屬性上,對不對? – user3137167