Rob85
Rob85

Reputation: 1729

WPF passing a bool to the view and changing it

How do i pass a bool from the viewmodel to the view and change the value. For example in my viewmodel i have created a bool

public bool load
{
    get { return m_load; }
    set
    {
        m_load = value;
        OnPropertyChanged(this, o => o.load);
    }
}

I then have my SelectedVm code

public ViewModel SelectedVm
{
    get { return _selectedVm; }
    set
    {        
        _selectedVm = value;
        if (_selectedVm != null && load == true)
        {
            _selectedVm.Load();                   
        }
        Load = false;
        OnPropertyChanged(this, o => o.SelectedVm);               
    }
}

In my View the SelectedVm is bound twice but only on one of the bindings do i want Load to be called, hence the need to change the bool load

So in my view if i have the following

<ListView Grid.Row="1"  Name="Sample"  
          ItemsSource="{Binding Path=SampleViewModel}"  
          SelectedItem="{Binding SelectedVm, Mode=TwoWay}"     
          IsSynchronizedWithCurrentItem="True" Width="500">            
</ListView>

How do i change the bool load to either true or false All of the above are just quick samples, i think this is probably quite simple however i am not that used to WPF and am still learning. any advice would be great

Upvotes: 1

Views: 1477

Answers (2)

Liel
Liel

Reputation: 2437

The code you published ensures that Load() is being called once, even with multiple bindings.

If I understand your real question correctly, you are actually asking for a way to make sure that each SelectedVM is calling Load() function once, and only once. Right?
if that so, you need to add a bool property to the ViewModel class, instead of the main class, that's all.

And then:

public ViewModel SelectedVm
{
    get { return _selectedVm; }
    set
    {        
        _selectedVm = value;
        if (_selectedVm != null && _selectedVm.load == true)
        {
           _selectedVm.Load();                   
           _selectedVm.load = false;
        }
        OnPropertyChanged(this, o => o.SelectedVm);               
    }
}

you can keep your XAML as it is.

Upvotes: 0

yo chauhan
yo chauhan

Reputation: 12295

Ok if you want to get value of Load in View and you want to do it in pure MVVM pattern then create DependencyProperty of bool type in View and Bind it to Load property of VM like

public partial class MainWindow : Window
{
    public static readonly DependencyProperty LoadProperty = DependencyProperty.Register("MyCustom", typeof(bool), typeof(MainWindow), new PropertyMetadata(new PropertyChangedCallback(LoadPropertyChangedCallback)));

    public bool Load
    {
        get
        {
            return (bool)this.GetValue(LoadProperty) ;
        }
        set
        {
            this.SetValue(LoadProperty, value);
        }
    }

    static void LoadPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    { 
        //Do your load stuff here
    }

    public MainWindow()
    {
        InitializeComponent();
        this.SetBinding(LoadProperty, new Binding("load"));
        DataContext = new ViewModel();
    }
}
public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        load = true;
    }
    bool m_load;
    public bool load
    {
        get { return m_load; }
        set
        {
            m_load = value;
            OnPropertyChanged("load");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

Upvotes: 1

Related Questions