Dmitriy
Dmitriy

Reputation: 664

Binding UpdateSource event

I use editable label control (label with overrided template property; full code you see there: http://www.nathanpjones.com/wp/2012/09/editable-label-in-wpf/ ).

I create own label contol class with this style. EditableLabelControl class extends Label class, so EditableLabelControl has Content property.

public partial class EditableLabelControl : Label
{
    //...
}

After that I put this editable label in my custom control and bind it to model's MyValue property.

<controls:EditableLabelControl Content="{Binding Path=MyValue, Mode=TwoWay}" />

It correctly displays model value, but when I made edits in textbox it updates Content property only (model's MyValue property doesn't updates).

I tryed to write LostFocus handler for textbox, but it doesn't help.

var bindingExpression = ((TextBox)sender).GetBindingExpression(TextBox.TextProperty);
if (bindingExpression != null)
{
    bindingExpression.UpdateSource();
}

Where is my mistake? Thanks for answers and sorry for my bad english.

Upvotes: 0

Views: 638

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43596

Maybe you could try setting the UpdateSourceTrigger to PropertyChanged, this should update your label whenever the textbox property changes.

example:

    <StackPanel>
        <Label Content="{Binding ElementName=UI, Path=MyValue, UpdateSourceTrigger=PropertyChanged}" x:Name="label"/>
        <TextBox Text="{Binding ElementName=UI, Path=MyValue, UpdateSourceTrigger=PropertyChanged}" x:Name="textbox" />
    </StackPanel>

code:

    public partial class EditableLabelControl : Label, INotifyPropertyChanged
    {
        private string _myValue;
        public string MyValue
        {
            get { return _myValue; }
            set { _myValue = value; NotifyPropertyChanged("MyValue"); }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Notifies the property changed.
        /// </summary>
        /// <param name="property">The info.</param>
        public void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }

Upvotes: 1

Related Questions