locked
How can i make dynamic data binding ? RRS feed

  • Question

  • hello everyone :)

    I'm working on a simple app and i needed to add data into list box dynamically ( data will be changed while runtime ) i used data binding  but the problem is that it can be initialized once and i need to modify it several times is there an easy way to do that task ?

    i have Xaml code just like 

    <ListBox.ItemTemplate>
                    <DataTemplate >
                        <StackPanel  Orientation="Horizontal">
                            <TextBlock x:Name="startT" Text="{Binding start_time}"  TextWrapping="Wrap" FontSize="22" Width="Auto" Height="Auto"  Margin="0,0,20,0"  />
                            <TextBlock x:Name="EndT" Text="{Binding end_time}"  TextWrapping="Wrap" FontSize="22" Width="Auto" Height="Auto" Margin="0,0,20,0" />
                            <TextBlock x:Name="TextT" Text="{Binding text}" TextWrapping="Wrap" FontSize="22" Width="Auto" Height="Auto"  />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

    and that's my data class

            

     class line 
        {

            public string start_time
            {
                get;
                set;
            }

            public string end_time
            {
                get;
                set;
            }
            public string text
            {
                get;
                set;
            }

            public line(string s, string e, string t)
            {
                start_time = s;
                end_time = e;
                text = t;
            }

    Thanks in advance :)

    Thursday, November 28, 2013 9:25 PM

Answers

  • The way to update the binding target (in your case TextBlock.Text) is to implement the INotifyPropertyChanged interface on your data object (class line) and then raise the RaisePropertyChanged event when the value of the binding source (start_time, etc.) changes. This way WPF binding will know that data changed and it will update UI automatically.

    This is an example of implementing the INotifyPropertyChanged  interface:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
     
    namespace WBS.DataLayerSimple
    {
        public class BaseObject : INotifyPropertyChanged
        {
            // Tracking changes
            public bool IsDirty { get; set; }
     
            // Implementation of INotifyPropertyChanged
            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
            {
                var handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, e);
                }
            }
     
            protected void RaisePropertyChanged(string propertyName)
            {
                OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    then you can inherit from ObjectBase and raise the RaisePropertyChanged event. The 'Book' class would be your 'line' class:

    using System;
     
    namespace WBS.DataLayerSimple
    {
        public class Book : BaseObject
        {
            private string title;
            public string Title
            {
                get
                {
                    return title;
                }
                set
                {
                    if (title != value)
                    {
                        title = value;
                        IsDirty = true;
                        RaisePropertyChanged("Title");
                    }
                }
            }
     
            private string authors;
            public string Authors
            {
                get
                {
                    return authors;
                }
                set
                {
                    if (authors != value)
                    {
                        authors = value;
                        IsDirty = true;
                        RaisePropertyChanged("Authors");
                    }
                }
            }
     
            private string isbn13;
            public string Isbn13
            {
                get
                {
                    return isbn13;
                }
                set
                {
                    if (isbn13 != value)
                    {
                        isbn13 = value;
                        IsDirty = true;
                        RaisePropertyChanged("Isbn13");
                    }
                }
            }
            
        }
    }

    The above code was taken from: http://wbswiki.com/doku.php?id=notes:wpf:datalayersimple

    Leszek


    Wiki: wbswiki.com
    Website: www.wisenheimerbrainstorm.com


    • Edited by ata6502 Thursday, November 28, 2013 9:43 PM
    • Marked as answer by Andrew Ayoub Thursday, November 28, 2013 9:47 PM
    Thursday, November 28, 2013 9:41 PM