spacebarisdefect
spacebarisdefect

Reputation: 75

WPF C# Binding behaving strangely

So I have a c# wpf application with a default layout and different UserControls to fill one part of that layout. So far everything worked like a charm with binding properties, but now that i created another UserControl the binding only seems to work OneWay.

View -> ViewModel works great, I can trace button clicks, comboboxes being checked and all that stuff, but ...

ViewModel -> View doesn't want to work at all.

I've tried setting the Mode of the Bindings to TwoWay and setting UpdateSourceTrigger to PropertyChanged, but nothing changes.

This is my View:

<UserControl ...
      xmlns:vm="clr-namespace:Prueftool.BBCCreatorViewModel"
      d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.DataContext>
        <vm:CreateDisplayTypeViewModel/>
    </UserControl.DataContext>

    <Grid>
        <Button Content="Button" Width="75" Command="{Binding TestButtonClick}"/>
        <CheckBox Content="CheckBox" IsChecked="{Binding TestIsChecked}"/>

    </Grid>
</UserControl>

And here is my referenced ViewModel:

namespace Prueftool.BBCCreatorViewModel
{
    class CreateDisplayTypeViewModel : ViewModelBase, ICreateDisplayViewModel
    {

        private bool _testIsChecked;
        public bool TestIsChecked
        {
            get { return _testIsChecked; }
            set
            {
                _testIsChecked = value;
                OnPropertyChanged("TestIsChecked");              
            }
        }

        public void SetNewDisplayType(DisplayType selectedDisplayType)
        {

            if(selectedDisplayType.Name == "Default")
            {

                TestIsChecked = true;    
            }

        }


        private DelegateCommand _random;
        public ICommand RandomButtonClick
        {
            get
            {
                if (_random == null)
                {
                    _random = new DelegateCommand(randomButtonClick);
                }
                return _random;
            }
        }


        private void randomButtonClick()
        {
            if(TestIsChecked)
            {
                MessageBox.Show("Hello World");
            }
        }

    } 
}

The SetNewDisplayType method is being called and the if statement is true, but it won't check my combobox in the view. On the other hand, checking the combobox manually and then pressing the button fires the randomButtonClick method and a MessageBox appears.

EDIT:

OnPropertyChanged method (not mine)

#region public virtual void OnPropertyChanged()
        /// <summary>
        /// Raises this object's PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The property that has a new value.</param>
        public virtual void OnPropertyChanged(string propertyName)
        {
            this.VerifyPropertyName(propertyName);

            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
        #endregion

Upvotes: 0

Views: 102

Answers (2)

MaSiMan
MaSiMan

Reputation: 704

I think you may be calling SetNewDisplayType on a different instance of CreateDisplayTypeViewModel than the one used as DataContext. The binding works and the checkbox is checked when I use your UserControl and change the Constructor to

public MyUserControl()
{
    InitializeComponent();
    ((CreateDisplayTypeViewModel)DataContext).SetNewDisplayType();
}

and SetNewDisplayType to

    public void SetNewDisplayType()
    {
            TestIsChecked = true;    
    }

It would help though if you could post how this function is called.

Edit: The fact that the handler in OnPropertyChanged is null (as you mentioned in the comments above) is also a hint that you might be using two instances of the VM.

Upvotes: 1

Craig
Craig

Reputation: 693

I think you just need to implement INotifyPropertyChanged on your class.

class CreateDisplayTypeViewModel : ViewModelBase, ICreateDisplayViewModel, INotifyPropertyChanged

I see you have the OnPropertyChanged method but you would also need to implement to PropertyChangedEventHandler. Something like this should do it:

#region Public Events

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion Public Events

    #region Protected Methods

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

    #endregion Protected Methods

Upvotes: 0

Related Questions