Sunday, June 6, 2010

Binding with Element using code


You can also set Element binding using Code. Below is sample code for binding with element in code.
C# Code behind code
namespace WPF.DataBinding.Sample.CS
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            

            Binding binding=new Binding();
            binding.ElementName="mySliderCode";
            binding.Path = new PropertyPath("Value");
            lblSelectdValue.SetBinding(Label.ContentProperty, binding);
        }
    }
}
XAML Code :

            
                
                
            
            
            
                
                
            
        

Saturday, June 5, 2010

Data binding with Element in XAML


In WPF you can also bind data to property of any controls. Like if you have slider and want to bind value property to label then you can use data binding. You can use following code to bind data to elements.

            
                
                
            
            
            
                
                
            
        
In above code you can see we have to use two property of Binding class. ElementName which specify to which element we need to do databinding and path specifies which property we need to bind of that element.

Data binding in WPF


Binding is class which is used for data binding in WPF. Binding class has below important property which you can use for data binding in WPF.
  • ElementName : Name of element to which you want to bind
  • FallbackValue: It is used when binding is unavailable. You can say default value. Like if you have set binding to one element’s “ABC” property but if class does not contain “ABC” property then it will use FallbackValue.
  • Mode: It defines the mode of data binding.
  • NotifyOnSourceUpdated: It specifies whether we need to fire event sourceupdated when value is transferred from target to source.
  • NotifyOnTargetUpdated: It specifies whether we need to fire event sourceupdated when value is transferred from source to target.
  • Path: It specify property name with which we need to bind data.
  • XPath: specifies XPath query which will be used when retrieving value from XML.
  • Source: It specify object which we need to bind.
  • RelativeSource: Specify binding’s location relative source. If you want to bind control by using visual tree then you can specify relative source of binding.
Data binding with different type.