WPF Learning 03:element Binding

Source: Internet
Author: User

Element bindings are a subset of data bindings, and there are a lot of articles in the garden that are very good, and are not explained in detail here.

WPF implements a perfect data binding mechanism, which makes element binding easy to implement.

The elements of this article refer to Visual controls in WPF.

Example

XAML Code:

<Windowx:class= "Elementbinding.mainwindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= " the"Width= "525">    <StackPanel>        <SliderName= "Fontsizeslider"Minimum= "Ten"Maximum= "Max"></Slider>        <TextBlockName= "TextBlock"FontSize="{Binding elementname=fontsizeslider, path=value}">Hello WPF</TextBlock>    </StackPanel></Window>

Effect:

  

  

binding Mode

In the example above, fontsize= "{Binding elementname=fontsizeslider, Path=value}" is the code required to bind the element.

We can use C # code to achieve the same binding, the effect is the same:

Private void setbinding () {    varnew  Binding ()    {       = Fontsizeslider,        New PropertyPath ("Value")      };    Textblock.setbinding (System.Windows.Controls.TextBlock.FontSizeProperty, bindingsetting);}

For a property, configure the binding, at this point, the element that configures the binding is the destination element in this binding, elementname specifies the source element, and path specifies the corresponding property of the source element bound to the FontSize property, in this case the slider's value.

Now let's make a change to the code: Add a button and increase the Handler of the button Click.

C # code:

Private void Button_Click (object  sender, RoutedEventArgs e) {    ;}

When we click the button, we can see that the text is bigger, but the position of the slider is not changed, which seems inconsistent with the expectation, which is related to the binding mode of data binding.

  

Is the three common modes of WPF data binding: The Textblock.fontsize default is the OneWay mode, which results in just the same effect.

  

We only need to add a mode=twoway to the XAML code to achieve the desired effect:

<Name= "TextBlock"  FontSize= "{Binding elementname= Fontsizeslider, Path=value, mode=twoway}">Hello Wpf</TextBlock >

In WPF's own control, the default mode is oneway except for the TextBox.Text default mode of TwoWay. Nonetheless, the display indicates that mode is still a good way to encode.

In the usual three modes, there is another mode: OneTime. The data binding can be transferred only if the form is initialized or the display is called on the binding expression to update.

Example:

XAML Code:

<SliderName= "Fontsizeslider"Minimum= "Ten"Maximum= "Max"></Slider><TextBlockName= "TextBlock"FontSize="{Binding elementname=fontsizeslider, Path=value, mode=onetime}">Hello WPF</TextBlock><ButtonClick= "Button_Click">Click Me</Button>

C # code:

Private void Button_Click (object  sender, RoutedEventArgs e) {    // Only call this sentence, The value of the slider is mapped to fontsize    textblock.getbindingexpression (textbox.fontsizeproperty). Updatetarget ();}

Trigger Mode

We'll make changes to the code to bind the input values of the FontSize and textbox:

<TextBoxName= "Fontsizebox"Text="{Binding elementname=textblock, path=fontsize}"></TextBox><TextBlockName= "TextBlock">Hello WPF</TextBlock><ButtonClick= "Button_Click">Click Me</Button>

We will find that it is still not possible to do so, and the input parameters cannot cause a change in font size. In fact, when you're done typing, we can update the font size by pressing TAB so that the input box loses focus. The reason: The default binding trigger mode for TextBox.Text is LostFocus.

The three triggering modes are as follows:

   

So, we just change the code to:

<Name= "Fontsizebox"  Text= "{Binding elementname= TextBlock, Path=fontsize, updatesourcetrigger=propertychanged}"></TextBox >

Enables real-time data binding.

How to debug

In the previous example, if we enter non-numbers in the input box, such as "AA", data binding does not throw any exceptions, how can we be sure that there is nothing wrong with data binding?

WPF is outputting data-bound errors ....

  

WPF Learning 03:element Binding

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.