Recommended

Skip Navigation LinksHome / Tips / Binding

Binding

+
Items Resolution

  • 3 comments  /  posted by  Emil Stoychev  on  Mar 23, 2009 (more than a year ago)

    Silverlight 3 enables property binding to CLR objects and other UI components via XAML – UI to UI binding. It is useful in a lot of scenarios and saves time for both developers and designers.

    Example

    Download source code

    Syntax

    {Binding Value, ElementName=MySlider}

    where Value is property of a CLR object and MySlider is the name of this object

    Hope that helps!

    Share


  • 0 comments  /  posted by  Denislav Savkov  on  Sep 16, 2008 (more than a year ago)

    Typically validation occurs when in a two-way binding the business object (source property) is updated with data from the user input (target property). During the update there are two places where validation occurs.

    1. First, the binding engine uses a implementation of IValueConverter to convert the data from one type to the other. Usually this converter has some validation. Unfortunately, Silverlight doesn't provide a way for custom validation for binding, like in WPF. There is no ValidationRule class that is used in WPF to provide the custom rule for the validation.
    Share
  • 4 comments  /  posted by  Martin Mihaylov  on  Sep 09, 2008 (more than a year ago)

    If you're not familiar with the value converters read this. The methods generated by the VisualStudio when creating a custom class that implements System.Windows.Data.IValueConverter have several arguments. One of them is of type object and is called parameter.

     

    public class DateTimeConverter : System.Windows.Data.IValueConverter
    {
        public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )...
       
        public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )...
    }

     

    In this example we bind to an object of type Book:

    public class Book
    {
        public DateTime PublishDate { get; set; }
    }

    We can pass this argument from the code or from the Xaml.

    Share
  • 1 comments  /  posted by  Martin Mihaylov  on  Sep 09, 2008 (more than a year ago)

    First let's explain what the convertors can be used for. Imagine you bind to an object's property, but the property is not formatted to your likings. In this case you can use converters. For example we bind to the PublishDate property of a Book object and want the date to be formatted like this - "dd MMM, yyyy".

    Book myBook = new Book();
    myBook.PublishDate = DateTime.Now;

    First let's create our converter.

    Share
  • 0 comments  /  posted by  Martin Mihaylov  on  Sep 09, 2008 (more than a year ago)

    If you want to bind an object's property to a control's property you can do it the following way.First let's create the object taht the control will bind to (for example a class called Book):

    public class Book
    {
        public string Title { get; set; }
        public string Description { get; set; }
    }

    After that define your control in the Xaml (a TextBlock for example):

    <TextBlock x:Name="MyText" Text="{Binding Title}"></TextBlock>

    Using the binding syntax we bind the Text proeprty to the Title property of the Book object. In the codebehind we create an instance of the Book object and set the DataContext of the control to it:

    Book myBook = new Book();
    myBook.Title = "Silverlight book";
    myBook.Description = "A book about Silverlight.
    Share
  • 2 comments  /  posted by  Denislav Savkov  on  Aug 29, 2008 (more than a year ago)


    Path is used to specify then name of the property of the underlying object to bind to. Additional you are able to use indirect property targeting to specify a sub-property of a property of the object. Currently it is not possible to bind to indexed properties.

    Xaml

    <TextBlock Text="{Binding Name}"/>
    <TextBlock Text="{Binding Path=Name}"/>
    <TextBlock Text="{Binding Path=Account.OpenDate}"/>
    <TextBlock Text="{Binding Path=Property1.Property2.Property3}"/>

    That's it!

    Share
  • 1 comments  /  posted by  Denislav Savkov  on  Aug 29, 2008 (more than a year ago)


    Currently there is no way to do that by using binding. Instead, you can make a List<> and add the values from the enumeration in the list. Then you can use this list as data source.

    C#

    List<Dock> enumDataSource = new List<Dock>() { Dock.Left, Dock.Top, Dock.Right, Dock.Bottom };
    this.lbDock.ItemsSource = enumDataSource;

    where lbDock is of type ListBox.

    Share
  • 0 comments  /  posted by  Denislav Savkov  on  Aug 29, 2008 (more than a year ago)

    When the value of a property is changed it should notify the bound objects that the property value has changed. You can do that by implementing changed event.

    C#

     

    public class Client
    {
        private string name;
        public event EventHandler NameChanged;
     
        public string Name
        {
            get
            {
                return this.Name;
            }
            set
            {
                if ( this.name == value )
                    return;
     
                this.name = value;
                this.OnNameChanged( EventArgs.Empty );
            }
        }
     
        protected virtual void OnNameChanged( EventArgs e )
        {
            if ( this.NameChanged != null )
                this.NameChanged( this, e );
        }
    }

     

    Note that the value is changed only if the new value is different.

    Share
  • 6 comments  /  posted by  Denislav Savkov  on  Aug 29, 2008 (more than a year ago)

    INotifyPropertyChanged interface is used to notify that a property has been changed and thus to force the bound objects to take the new value.

    C#

    public class Client : INotifyPropertyChanged
    {
        private string name;
        public event PropertyChangedEventHandler PropertyChanged;
     
        public string Name
        {
            get
            {
                return this.Name;
            }
            set
            {
                if ( this.name == value )
                    return;
     
                this.name = value;
                this.OnPropertyChanged( new PropertyChangedEventArgs( "Name" ) );
            }
        }
     
        protected virtual void OnPropertyChanged( PropertyChangedEventArgs e )
        {
            if ( this.PropertyChanged != null )
                this.PropertyChanged( this, e );
        }
    }

    Note that the value is changed only if the new value is different.

    Share

Help us make SilverlightShow even better and win a free t-shirt. Whether you'd like to suggest a change in the structure, content organization, section layout or any other aspect of SilverlightShow appearance - we'd love to hear from you! Need a material (article, tutorial, or other) on a specific topic? Let us know and SilverlightShow content authors will work to have that prepared for you. (hide this)