0

I am a doing a small WPF MVVM program that:

  1. Has a MainWindow with Label (saying "Hello") and a button to open another window (I did the open window part in code behind).
  2. This opens another window with 2 radio buttons (Red and Blue) and a cancel button (I did close function in code behind).
  3. If I press the Red radio button, the Label on MainWindow should turn Red, similarly with pressing the Blue radio button.

Can somebody help me with this? I am sort of new to WPF and completely new to MVVM methodology. I am posting my ViewModel code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using PocMVVM.Views;


namespace PocMVVM.ViewModel

{

    public class ColorChangeViewModel : ICommand

    {

        //ColorChoiceView colorSelect = new ColorChoiceView();
        //MainWindow mw = new MainWindow();
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
            //throw new NotImplementedException();
        }

        public void Execute(object parameter)
        {
           //throw new NotImplementedException();
           ColorChoiceView colorSelect = new ColorChoiceView();
           MainWindow mw = new MainWindow();    
           if((bool)parameter==colorSelect.RedButton.IsChecked.Equals(true)) 
           {
                mw.label.Foreground = Brushes.Red;
                mw.UpdateLayout();
                mw.ShowDialog();
            }
            else if((bool)parameter ==  colorSelect.BlueButton.IsChecked.Equals(true))
            {
                mw.label.Foreground = Brushes.Blue;
                mw.UpdateLayout();
                mw.ShowDialog();
            }
        }
    }
}

Can somebody help me with this?

Thank You Very Much!!

P.S. I know people might ask about the need for two windows, but it has to be this way. I was told it had to be like this, so no other way.

Pikoh
  • 7,582
  • 28
  • 53
  • Where i syour view code? and why are you referencing your MainWindow in ViewModel? – N.J Oct 06 '16 at 09:04
  • Isn't that the only way to access the label? – Kaushik Ravi Oct 06 '16 at 09:16
  • No its is not the way to access. if you are following MVVM Use Databindings, Commands etc. I would suggest you read more about MVVM Pattern and WPF before you write your code – N.J Oct 06 '16 at 11:23

1 Answers1

0

First of all, you should NOT have a reference to the view in your viewmodel. According to the MVVM pattern, you should set the DataContext of the view to your viewmodel, but the viewmodel should know nothing of the view. If you want your command to open a window, you can use services. Take a look at this: Opening new window in MVVM WPF

Furthermore, you can have a binding to the foreground color. In your viewmodel you should have something like this:

public System.Windows.Media.Brush ForegroundColor
{
    get { return _foregroundColor; }
    set
    {
        _foregroundColor = value;
        OnPropertyChanged("ForegroundColor");
    }
}

And in the XAML with the Hello label:

 <TextBlock Foreground="{Binding Path=ForegroundColor, Mode=TwoWay}" />

And for the radio button you should have an ICommand to respond when you switch between the two colors and further set your property ForegroundColor to this value. Like this (in XAML):

<RadioButton Content="Red" Command="{Binding SwitchButtonCommand}" CommandParameter="Red" />
<RadioButton Content="Blue" Command="{Binding SwitchButtonCommand}" CommandParameter="Blue" />

And in the implementation of the SwitchButtonCommand command you can set ForegroundColor to red or blue, depending on the parameter.

Community
  • 1
  • 1
Michelle
  • 56
  • 3
  • But how is this linked to the radio button? The main objective is to use the radio buttons to change the Label color? – Kaushik Ravi Oct 06 '16 at 09:42
  • And btw what is OnPropertyChanged()? And how do I implement it? – Kaushik Ravi Oct 06 '16 at 10:36
  • I've edited my answer , but I would suggest you read first an introduction to bindings, INotifyPropertyChanged etc, otherwise it doesn't make sense to use MVVM. Take a look here for example: http://www.learnmvvm.com/ – Michelle Oct 06 '16 at 11:44
  • Something like this? private ICommand switchButtonCommand; public ICommand SwitchButtonCommand { get { return switchButtonCommand; } set { if(switchButtonCommand == null) { ForegroundColor = Brushes.Red; } } } – Kaushik Ravi Oct 06 '16 at 13:20