Bindings on a ContentControl do not update the data

1.1k views Asked by At

I have the code:

<TextBox Width="200" Text="{Binding Value}"></TextBox>

Which works. However the "Value" can be different types. So if I have an bool I want to show a checkbox. I rewrote it as as follow, which kinda works:

<ContentControl Content="{Binding Value}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type sys:Boolean}">
            <CheckBox IsChecked="{Binding Path=.}"></CheckBox>
        </DataTemplate>
        <DataTemplate DataType="{x:Type sys:Double}">
            <TextBox Width="200" Text="{Binding Path=.}"></TextBox>
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

But now the property isn't updated like before. I have tried setting Mode=Twoway, but it still do not work.

Edit

It worked perfectly fine when I only had the textbox, editing the text of the textbox updated the model. However when I tried doing this with the second code (ContentControl) it just doesn't work.

Code

I'm using Mvvm-light togheter with bindings. The "Value" is bound to an instance of Property

    [JsonObject]
    public class Property<T> : INotifyPropertyChanged
    {
        [JsonProperty]
        public String name;

        public Property(String name, T value)
        {
            this._value = value;
            this.name = name;
        }

        [JsonIgnore]
        public T Value {
            get { return _value; }
            set {
                _value = value;
                hot = true;
                NotifyPropertyChanged("Value");
            }
        }

        [JsonProperty(PropertyName = "value")]
        private T _value;

        [JsonIgnore]
        public String Name { get { return name; } set { name = value; } }

        [JsonProperty]
        public bool hot = false;

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
1

There are 1 answers

2
Abdurrahman Alp Köken On

You should implement INotifyPropertyChanged interface in order to track property changes. I'm sure everything works fine then.

This works for me:

 public partial class MainWindow : Window, INotifyPropertyChanged
{
    private object value;        

    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
        DataContext = this;
    }

    public object Value
    {
        get { return value; }
        set
        {                
            this.value = value;
            NotifyPropertyChanged("Value");
        }
    }       

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Value = true;            
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}