1

I Have UserControl called "Footer.xaml" and "Header.xaml" Both User Control are place to different window.

Footer.xaml have two button :- btnBasic btnStandard

Header.xaml have one button :- lobby

When i click on Lobby button from the Header.xaml i want to change the IsEnabled property of the both button [ btnBasic and btnStandard ] on my condition.

I Try the below things [ Footer.xaml.cs ] by default the both button IsEnabled = true

public partial class Footer : UserControl
    {

        static Footer objFooter = new Footer();
        public Footer()
        {
            InitializeComponent();
            objFooter = this;

        }


        public static Footer GetFooterInstance()
        {
            return objFooter;
        }
}

and on Header.xaml.cs

private void btnLobby_Click(object sender, RoutedEventArgs e)
        {
                Footer objFooter;
                objFooter = Footer.GetFooterInstance();
                objFooter.btnBasic.IsEnabled = false;
                objFooter.btnStandard.IsEnabled = false;
    }

But nothings is effect with button.

ujjaval
  • 1,523
  • 4
  • 20
  • 35
  • 1
    There's no MVVM pattern in your scheme. Are you asking for a real MVVM, or rather do you want to solve in the most direct way? – Mario Vernari Sep 21 '13 at 06:57
  • @Mario Vernari :- Real MVVM i have View for FooterViewModel and same for HeaderViewModel – ujjaval Sep 21 '13 at 06:59
  • Why you hold the static instance in your class? What you want to achieve? If you have the something like `
    ` in your `XAML` then it is other instance rather than returned by `Footer.GetFooterInstance()`.
    – Hamlet Hakobyan Sep 21 '13 at 07:00
  • 1
    @ujjaval: if you want a solution for your MVVM-based project, you should post the MVVM source as well. As some users stated, the ICommand surrogate (e.g. RelayCommand) is the solution for a elegant yet robust MVVM architecture. – Mario Vernari Sep 21 '13 at 07:10

1 Answers1

2

You tagged your question for MVVM but posted code is completely violating the rules of MVVM here. You can achieve this by stricting to the rules of MVVM in following manner -

  1. Create a ViewModel class which will serve as DataContext for both of your views.

  2. Create a bool property inside it and bind IsEnabled DP for your buttons namely btnBasic and btnStandard with this property.

  3. Create an ICommand in your ViewModel class which will be invoked on lobby button click and will set this bool property to true or false depending on your situation.

But as you posted in comment above, you already have seperate ViewModels for both Views, you can use Event Aggregator to communicate between two ViewModels.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185