HoboSteaux
HoboSteaux

Reputation: 1

C# Bindings not working to Property<T>

I am currently trying to follow MVVM in C# 4, but having troubles with the bindings working.

Starting from the bottom, here is my Property class that should take care of the property changed for XAML bindings:

namespace Visualizer.MVVM
{
    public class Property<T> : DependencyObject, INotifyPropertyChanged
    {
        //private T _Value;
        public T Value
        {
            get { return (T) GetValue(ValueProperty); }
            set
            {
                SetValue(ValueProperty, value);
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged()
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs("Value"));
            }
        }

        public Property(T val)
        {
            Value = val;
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(T), typeof(Property<T>));
    }
}

My ViewModel for the control looks like this and is instantiated in MainWindow.xaml.cs:

public class CheckboxControlVM
{
    public Property<bool> IsChecked { get; set; }
    public Property<string> Name { get; set; }

    public CheckboxControlVM(bool isChecked, string name)
    {
        IsChecked = new Property<bool>(isChecked);
        Name = new Property<string>(name);
    }
}

The control has no code-behind, so here is the XAML for it:

<UserControl x:Class="Visualizer.MVVM.Checkbox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Visualizer.MVVM"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel Orientation="Horizontal" x:Name="LayoutRoot">
        <CheckBox IsChecked="{Binding Path=IsChecked.Value, Mode=TwoWay}"/>
        <TextBlock Text="{Binding Path=Name.Value, Mode=OneWay}"/>
    </StackPanel>
</UserControl>

Finally, here is the binding in MainWindow.xaml:

<mvvm:Checkbox DataContext="{Binding Realtime}"/>

I have been stuck on this for a lot longer than I should be and am fairly certain its just a simple issue. Any ideas?

Upvotes: 0

Views: 131

Answers (2)

Fendy
Fendy

Reputation: 4643

I don't quite get your objective of what you want to achieve with that property design. Normally I don't do that in WPF so I'm not quite sure whether this help or not.

Usually, I do implement INotifyPropertyChanged in ViewModel level, not in the attribute owned by VM. Example:

public class ViewModel : INotifyPropertyChanged{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Second, I do not use DependencyProperty unless I make a WPF user control. So I use private property and trigger the OnPropertyChanged with the property name.

private string _name;
public string Name{
  set{
    _name = value;
    OnPropertyChanged("Name");
  }
}

Last, in the XAML, I use binding with UpdateSourceTrigger=PropertyChanged.

<TextBlock Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Maybe you can try to add UpdateSourceTrigger=PropertyChanged in your binding, but I am not sure if it will work.

Upvotes: 1

TalentTuner
TalentTuner

Reputation: 17556

try this

public class CheckboxControlVM
  {
    bool _isChecked = false;
    string _name ;
  public Property<bool> IsChecked { get { return _isChecked} set { _isChecked=value;} }

  public Property<string> Name { get { return _name } set { _name =value;} }

  public CheckboxControlVM(bool isChecked, string name)
  {
    _isChecked = isChecked;
     _name = name;
    IsChecked = new Property<bool>(_isChecked);
    Name = new Property<string>(_name);
  }
}

Upvotes: 0

Related Questions