none
Binding two text block by INotifyPropertChanged RRS feed

  • Question

  • hi

    <Page.DataContext>
            <local2:ImportPointsPageViewModel/>
    </Page.DataContext>
    
            <TextBlock x:Name="textBlock1" Width="460" Text="{Binding Path=Text, ElementName=textBox1, Mode=TwoWay}"/>
            <TextBox x:Name="textBox1" Height="30" Text="{Binding NewName}"/>

    the above code works exactly as i exepected, textBlock.Text synchronized with textBox1.Text.

    but when i'm trying to use mvvm and trying to synchronized those two control (same result of the code above), it dosent work and here is my code:

    <Page.DataContext>
            <local2:ImportPointsPageViewModel/>
    </Page.DataContext>
    
            <TextBlock x:Name="textBlock1" Width="460" Text="{Binding FileName}"/>
            <TextBox x:Name="textBox1" Height="30" Text="{Binding NewName , Mode=TwoWay}"/>

    and

    public class ImportPointsPageViewModel : ViewModelBase
        {
    
            private string _fileName="initial value";
            public string FileName
            {
                get { return _fileName; }
                set
                {
                    _fileName = value;
                    RaisePropertyChanged(nameof(FileName));
                    RaisePropertyChanged(nameof(NewName));
                }
            }
    
            private string _newName = "initial valuee";
    
            public string NewName
            {
                get 
                {
                    return _newName;
                }
                set 
                {
                    _newName = value;
                    FileName = NewName;
                    RaisePropertyChanged(nameof(NewName));
                    RaisePropertyChanged(nameof(FileName));
                }
            }
    
    
    
    
        }

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

    it doesn't work the same way, it does bind the two text textblocks but with no synchronizing ( it needs me to click somewhere to update the value of textBlock1.Text).

    thanks in advance,

    any help will be appreciated.



    • Edited by essamce Monday, December 2, 2019 7:48 PM
    Monday, December 2, 2019 7:45 PM

Answers

  • Hi,

    add UpdateSourceTrigger,updates the binding source immediately whenever the binding target property changes.

    <TextBox x:Name="textBox1" Height="30" Text="{Binding NewName , Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by essamce Tuesday, December 3, 2019 6:58 AM
    Tuesday, December 3, 2019 1:22 AM
    Moderator

All replies

  • Hi,

    add UpdateSourceTrigger,updates the binding source immediately whenever the binding target property changes.

    <TextBox x:Name="textBox1" Height="30" Text="{Binding NewName , Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by essamce Tuesday, December 3, 2019 6:58 AM
    Tuesday, December 3, 2019 1:22 AM
    Moderator
  • thanks Alex

    it works well,

    any recommendations to enhance my code ?

    Tuesday, December 3, 2019 6:59 AM