0

I am trying to hide button on Page2, on click of button on Page1 but it does not seem to work.

Page 1 : btnRemove

Page 2 : btnEdit

Below is code that i am trying.

 private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            Uri uri = new Uri("Page2.xaml", UriKind.Relative);
            this.NavigationService.Navigate(uri);
            Page2 page = new Page2();
            page.btnEdit.Visibility = Visibility.Hidden;
        }

What am i doing wrong??

Community
  • 1
  • 1
Richa
  • 3,261
  • 2
  • 27
  • 51
  • Bind your button visibility to a static field in your Page2, and from you page1, change this visibility (don't forget to implement INotifyPropertyChanged and set datacontext to work) – cdie Mar 30 '15 at 11:12
  • See: http://stackoverflow.com/questions/12444816/how-to-pass-values-parameters-between-xaml-pages – goobering Mar 30 '15 at 11:13
  • @cdie I am very new to WPF ,can you please elaborate a bit more.Thanks – Richa Mar 30 '15 at 11:14

1 Answers1

1

Go to your XAML code change the button definition by adding the following

Visibility="{binding ButtonVisibility}"

create a new class called ButtonViewModel.cs here is the code

class ButtonViewModel: INotifyPropertyChanged
{

    public ButtonViewModel(Visibility visibility)
    {
        _buttonVisibility = visibility;
    }
    private Visibility _buttonVisibility ;

    public Visibility ButtonVisibility
    {
        get { return _buttonVisibility; }
        set
        {
            _buttonVisibility = value;
            OnPropertyChanged("ButtonVisibility");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string p)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(p));
    }
}

in page2.cs add this constructor

 public Page2(Visibility visibilty)
    {
        InitializeComponent();
        DataContext = new ButtonViewModel(visibilty);
    }
    public Page2()
    {
        InitializeComponent();
        DataContext = new ButtonViewModel(Visibility.Visible);
    }

go to the button event handler and add this code

        NavigationWindow nvw = new NavigationWindow();
        nvw.Content = new Page1(Visibility.Collapsed);
        nvw.Show();

it is working as it should with me

Coder1409
  • 523
  • 4
  • 12