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.

4
  • 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? Commented Sep 21, 2013 at 6:57
  • @Mario Vernari :- Real MVVM i have View for FooterViewModel and same for HeaderViewModel Commented Sep 21, 2013 at 6:59
  • Why you hold the static instance in your class? What you want to achieve? If you have the something like <Footer/> in your XAML then it is other instance rather than returned by Footer.GetFooterInstance(). Commented Sep 21, 2013 at 7: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. Commented Sep 21, 2013 at 7:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can YOu please Give and working Example for it. Using ICommand
Look at here for sample implementation using RelayCommand - stackoverflow.com/a/1468830/632337

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.