Silverlight data binding-element property binding

Source: Internet
Author: User

For programmers, a lot of knowledge is better explained by code than by text. So when introducing the Silverlight data binding feature, I use the following code as a memorandum for future study ......

Don't try it on the masses ......

 1:  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
 2:              <Grid.RowDefinitions>
 3:                  <RowDefinition Height="*" />
 4:                  <RowDefinition Height="*" />
 5:                  <RowDefinition Height="*" />
 6:              </Grid.RowDefinitions>
 7:  
 8:              <Slider Name="slider" 
 9:  Value="90"
10:  Grid.Row="0"
11:  Maximum="180"
12:  Margin="24" />
13:  
14:              <TextBlock Name="txtblk" 
15:  Text="{Binding ElementName=slider, Path=Value}"
16:  Grid.Row="1"
17:  FontSize="48"
18:  HorizontalAlignment="Center"
19:  VerticalAlignment="Center" />
20:  

 

Note that the text binding code of the textbock attribute in the above Code can also be simplified in the following form: text = "{binding value, elementname = slider }".

You can also use the following alternative code: text = "{binding elementname = contentpanel, Path = children [0], value }".

It is worth noting that this binding syntax can be used without the attribute element Syntax:

 1:   <TextBlock Name="txtblk" 
 2:  Grid.Row="1"
 3:  FontSize="48"
 4:  HorizontalAlignment="Center"
 5:  VerticalAlignment="Center" >
 6:                  <TextBlock.Text>
 7:                      <Binding ElementName="slider" Path="Value"></Binding>
 8:                  </TextBlock.Text>
 9:              </TextBlock>
10:  
11:  

 

Silverlight Data Binding can also use C # code. Of course, it is also easy to ignore or use less, but it should also be paid attention!

 1:  using System;
 2:  using System.Collections.Generic;
 3:  using System.Linq;
 4:  using System.Net;
 5:  using System.Windows;
 6:  using System.Windows.Controls;
 7:  using System.Windows.Documents;
 8:  using System.Windows.Input;
 9:  using System.Windows.Media;
10:  using System.Windows.Media.Animation;
11:  using System.Windows.Shapes;
12:  using Microsoft.Phone.Controls;
13:  using System.Windows.Data;
14:   
15:  namespace SliderBindings
16:  {
17:      public partial class MainPage : PhoneApplicationPage
18:  {
19:          // Constructor
20:  public MainPage()
21:          {
22:              InitializeComponent();
23:              Binding binding = new Binding();
24:              binding.ElementName = "slider";
25:              binding.Path = new PropertyPath("Value");
26:              //1.this.txtblk.SetBinding(TextBlock.TextProperty,binding);
27:              //2.BindingOperations.SetBinding(this.txtblk,TextBlock.TextProperty,binding);
28:  }
29:      }
30:  }
31:   

 

The code above binds the attribute value of the silder element named slider with the attribute text of the textblock element named txtblk ......

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.