【. NET deep Breathing "(WPF) complete binding across Windows

Source: Internet
Author: User

In the case of 99.999975%, the mutual binding between the visual elements will be done in the same container, like a window, the same UserControl, etc. However, in rare cases, you can use cross-window binding in order to take into account the minimization of code assignments.

For example, there is a control in window A that has a YB control in the Xa,b window, and I want to bind the Text property of the XA control in a to the Item property of the YB control in B. The benefit of binding is that when the Item property is updated, the Text property is automatically modified without the need for additional write code to monitor changes to the properties of each control. Because high-level programmers always do the most things with the least amount of code.

However, by default, one window's code is inaccessible to the control in another window, and the control variable declared in the XAML document is accessed by default as private. To address this limitation, the fieldmodifier extension is provided in the extended markup for XAML to modify the way in which objects are declared in a XAML document. such as this:

        <Name= "Tbtxt"  x:fieldmodifier/>

By setting x:fieldmodifier= "public", the TextBlock control named Tbtxt is born into the public field, so that the control is accessible to other windows.

The Cross-window binding of a control can be achieved only if the above problem is resolved.

In order to make the content of this article lively, full of youthful vitality, the next week will give a 500 years in a simple example.

This example contains two windows, the main window is the MainWindow class, plus a custom window Mydialog (as a custom dialog box). The main window has a TextBlock control and a button control that opens the Mydialog dialog box when the button is clicked, and a ComboBox control in the dialog box, when the user selects an item in the ComboBox control. The display text for TextBlock in the main window is updated synchronously.

That is, the Text property of the Mainwindow.textblock object is bound to the SelectedItem property of the ComboBox control in Mydialog. The Text property automatically takes a value from the SelectedItem property.

In order for the Mydialog window to have access to the TextBlock object in the main window, the XAML writes like this:

        <Name= "Tbtxt"  x:fieldmodifier= "public"  Margin= "Ten"/>        <Content= "Open Dialog"   click = "Onopendialog" />

Then, design the Mydialog interface, declare the ComboBox object, and add several options.

        <ComboBoxName= "CMB" >            <sys:string>Plane</sys:string>            <sys:string>Tank</sys:string>            <sys:string>Train</sys:string>            <sys:string>Ship</sys:string>            <sys:string>Big Truck</sys:string>            <sys:string>Tractor</sys:string>            <sys:string>Armored vehicles</sys:string>        </ComboBox>        <ButtonHorizontalAlignment= "Center"Grid.Row= "1"Margin= "0,20"Content= "OK"Click= "OnClick"/>


Next, the binding is set through the code in the constructor of the Mydialog class.

         PublicMydialog (Window owner) { This. Owner =owner;            InitializeComponent (); //Set Binding ObjectBinding B =NewBinding (); B.path=NewPropertyPath (Combobox.selecteditemproperty); B.source=CMB; B.mode=Bindingmode.oneway; B.stringformat="the transport you have selected is: {0}"; MainWindow MW= Owner asMainWindow; //associating an attribute with a bindingMw?. Tbtxt.        SetBinding (Textblock.textproperty, b); }


The window class has an Owner property that can set its parent window, where Mydialog as a pop-up window, MainWindow as the parent window, so the owner is passed when the Mydialog is instantiated, allowing the code to access MainWindow. For convenience, I have directly written a constructor with parameters.

Note the binding here, the data source is a ComboBox control, the data consumer is TextBlock, so the source of the binding should be the CMB instance, and path is identified directly with the dependency property.

Finally, you can set the binding by calling the SetBinding method of the TextBlock object.

Why not use XAML to bind, because this is a cross-window binding, in XAML is difficult to write, in code to write rather good processing.

Run the program, and then open the dialog box and select an item in the dialog box, and you will see that the displayed text in the parent window is automatically updated.

To be precise, this article is not a high-tech, purely a skill to use it. The example does not need to be more complicated, as long as it can show the effect of the line, old Sunday hate to complicate things, so the old week's motto is:

Not easy to learn, non-combat without code.

Sample source Download

【. NET deep Breathing "(WPF) complete binding across Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.