user3137167
user3137167

Reputation: 5

Is this exception caused by the dependency property?

I don't understand why I get this exception when I run the app.

I've create a dependency property in MainWindow class, and in ImagesGrid class I called on this property.

I did not make any changes in xaml. Should I have binded something there too?

This is code for dependency 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));

this is where I call on the property

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

THis is constructor MainWindow

public MainWindow()
    {
        this.InitializeComponent();

        this.DataContext = this;

        ImagesDir = @".\GalleryImages";

    }

This is xaml code for button:

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

This is the image:

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

Upvotes: 0

Views: 113

Answers (1)

Clemens
Clemens

Reputation: 128013

You have set a wrong default value for the property, as its type is Visibility, not bool.

Change the declaration to

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

or leave out the property metadata completely:

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

You would bind the Visibility property of the button to the property like this:

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

Upvotes: 1

Related Questions