WPF binding properties of two-way change

  WPF dependency property binding properties can be achieved, after a successful bind modify the properties of the background as long as the bindings, you can synchronize automatically update the UI value binding, without the need to manually refresh the screen; the same, change the value of the front desk, by getting tied given attribute value may change a UI acquisition value, to achieve the effect of two-way change. Binding properties enable the UI update is very easy to share a small chestnut following description method used.

1, choosing to have a TextBlock and a UI of a Button, TextBlock want to achieve change after click.

<Window x:Class="WPFDemo.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFDemo"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300">
    
    <Grid>
        <Button Name="Button_OK" MaxWidth="50" MaxHeight="20" Click="Button_OK_Click">OK</Button>
        <TextBlock MaxWidth="50" MaxHeight="20" VerticalAlignment="Top"  HorizontalAlignment="Left" Text="{Binding Text}"></TextBlock>
    </Grid>
</Window>

 

2, create the UI update class (now the test, so the property is relatively small, the normal development of a recommended update UI UI to create a special class for UI update), the following is the complete code

public  class PropertyToUI: the INotifyPropertyChanged 
    { 
        #region private variable /// <Summary> /// status bar displays the text
         /// </ Summary> Private String text = "" ; #endregion #region properties /// <Summary> // / properties - display text
         /// </ Summary> public String the text 
        { GET { return text;}
             SET 
            { 
                text = 
                the OnPropertyChanged ( "
              
         
         
         

        

        
              
         
         
         
              value; the Text " ); 
            } 
        } 

        #endregion 

        #region attribute change notification event /// <Summary> /// attribute change notification event
         /// </ Summary> public Event PropertyChangedEventHandler the PropertyChanged; /// <Summary> // / attribute change notification
         /// </ Summary> /// <param name = "E"> </ param> public void the OnPropertyChanged (PropertyChangedEventArgs E) 
        { IF (the PropertyChanged! = null ) 
            {
        
         
         
         

         
         
         
         
             
                the PropertyChanged ( the this , E);
            }
        }

        /// <summary>
        /// 属性变化通知事件
        /// </summary>
        /// <param name="PropertyName"></param>
        public void OnPropertyChanged(string PropertyName)
        {
            PropertyChangedEventArgs e = new PropertyChangedEventArgs(PropertyName);
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }

        #endregion
    }

This section has the following key points:

(1), need to implement INotifyPropertyChanged interface, which is an attribute update interface, you can look at its implementation, there is a property update event, so to say declare the event.

namespace System.ComponentModel
{
    //
    // 摘要:
    //     Notifies clients that a property value has changed.
    public interface INotifyPropertyChanged
    {
        //
        // 摘要:
        //     Occurs when a property value changes.
        event PropertyChangedEventHandler PropertyChanged;
    }
}

(2), create a property update function

     /// <summary>
        /// 属性变化通知
        /// </summary>
        /// <param name="e"></param>
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }

Parameter update events of a property, then trigger PropertyChanged (this, e) notify the UI Updates the specified property

(3) packing properties

    public string Text
{
get { return text; } set { text = value; OnPropertyChanged("Text"); } }

Invoke the property setter update event, that is when the value set in the background (you want to update the UI value), it will trigger the update event property, notify the front desk bound dependency property is updated (event with attributes and identity value is passed).

 

3, the front desk dependency property to property update class attributes binding (Binding grammar)

<TextBlock MaxWidth="50" MaxHeight="20" VerticalAlignment="Top"  HorizontalAlignment="Left" Text="{Binding Text}"></TextBlock>

Binding to the property name

 

4, the binding of the data source (which is relatively easy to forget where)

The UI = PropertyToUI new new PropertyToUI ();
 the this .DataContext = the UI;   // event binding data sources

These are the steps necessary to attribute binding, and if no problem basically successful, not successful again, to check.

 

The following is the complete code behind:

///  <Summary> 
    /// the Window1.xaml the interaction logic
     ///  </ Summary> 
    public  partial  class the Window1: the Window 
    { 

        ///  <Summary> 
        /// the UI updates the class object
         ///  </ Summary> 
        PropertyToUI = the UI new new PropertyToUI (); 

        ///  <Summary> 
        /// constructor
         ///  </ Summary> 
        public the Window1 () 
        { 
            the InitializeComponent (); 

            the this .DataContext = the UI;   // event binding data source 

            UI.Text = " proceeding "; 
        } 

        ///  <Summary> 
        /// the OK button click event
         ///  </ Summary> 
        ///  <param name = "SENDER"> </ param> 
        ///  <param name = "E"> </ param> 
        Private  void Button_OK_Click ( Object SENDER, the RoutedEventArgs E) 
        { 
            UI.Text = " I updated " ; 
            MessageBox.Show (UI.Text); 
        } 
       
    }

 

Operating results are as follows:

 

Click the OK button:

 

Guess you like

Origin www.cnblogs.com/xiaomengshan/p/11564368.html