0

Is there a way to call methods from the view from the view model? Is this good practice to do so? If not, how would I hide elements in the view from the view model? I'm just a bit confused because I'm used to working with ASP.Net, with code behind, etc.

xaml.cs

btnsave.visibility = visibility.hidden;
btnclose.visibility = visibility.hidden;

1 Answer 1

2

For your specific example of hiding elements in the view, you probably want to set up some properties in the ViewModel that define the conditions under which those elements are visible. Then you bind the Visibility property (with a BooleanToVisibilityConverter, most likely) of those elements in the View to those properties in the ViewModel.

More generally, you want to keep the direct coupling between them minimal if you can, but sometimes "reality" gets in the way. I've had some cases where I've passed in the View to the constructor of the ViewModel. Other cases where it's been an interface that the View implements and that gets passed into the ViewModel. So there are options. But you should make sure you HAVE to go that route before doing it.

Example:

XAML:

<Window ...>
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="_B2VC" />
    </Window.Resources>
    <StackPanel>
        <Button Content="Save" Visibility="{Binding IsSaveButtonVisible}" />
        <Button Content="Close" Visibility="{Binding IsCloseButtonVisible}" />
    </StackPanel>
</Window>

ViewModel:

public class ViewModel: INotifyPropertyChanged
{

    #region INPC Stuff
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion

    private bool _IsSaveButtonVisible;
    public bool IsSaveButtonVisible
    {
        get { return _IsSaveButtonVisible; }
        set
        {
            if (_IsSaveButtonVisible != value)
            {
                _IsSaveButtonVisible = value;
                RaisePropertyChanged("IsSaveButtonVisible");
            }
        }
    }

    private bool _IsCloseButtonVisible;
    public bool IsCloseButtonVisible
    {
        get { return _IsCloseButtonVisible; }
        set
        {
            if (_IsCloseButtonVisible != value)
            {
                _IsCloseButtonVisible = value;
                RaisePropertyChanged("IsCloseButtonVisible");
            }
        }
    }
}

Then your ViewModel changes those properties in response to whatever it needs to (say for instance Save is only valid if they've changed something - once that something is changed, the property on the ViewModel gets updated and bam, that gets propogated to the View.

If you need further examples, i'd just suggest going and reading on MVVM. It takes a bit to grok, but its awesome once in use.

2
  • can you give me an example from the view model based on what i have updated?
    – Calvin
    Commented May 17, 2012 at 17:00
  • Before you ever consider passing your view into your viewmodel, take a look at the lightweight messaging capabilities that are offered by some of the ViewModel libraries, or write your own. Messaging can be tested-- passing your view into your viewmodel makes your VM code a lot less testable.
    – Robaticus
    Commented May 17, 2012 at 17:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.