Eran
Eran

Reputation: 79

silverlight binding


I'm writing a simple silver light application and having some problems with binding issues, I'm pretty much familiar with wpf binding but here I'm must doing something wrong,
well I started by binding collection to listbox item source but no binding was done and my PropertyChanged event was allways null I tried to use simple binding of text block to text property with no results the only way to view the text is xaml hard coded text
this is my data context class:
public class DataContextItems:INotifyPropertyChanged {

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    } 

    #region private members
    ObservableCollection<string> test;
    ObservableCollection<PlayListItem> _PlayList;
    PlayListItem[] _PlayListItems;
    #endregion

    #region public members
    public event PropertyChangedEventHandler PropertyChanged;
    public ObservableCollection<PlayListItem> PlayList 
    {
        get { return _PlayList; }
        set
        {
           _PlayList = value;
           OnPropertyChanged("PlayList"); 
        }
    }
    public PlayListItem[] PlayListItems
    {
        get { return _PlayListItems; }
        set { _PlayListItems = value; }
    }

    public Action<List<PlayListItem>> PlayListItemCallback 
    {
        get { return PlayListArrived; }
    }
    #endregion

    #region constructor
    public DataContextItems()
    {

        _PlayListItems = new PlayListItem[0];
        _PlayList = new ObservableCollection<PlayListItem>();
    }
    #endregion

    #region events
    void PlayListArrived(List<PlayListItem> playList)
    {
        foreach (PlayListItem item in playList)
            PlayList.Add(item);

    }
    #region events


}

the xaml:
controls:TabItem Header="search">
controls:TabItem.ContentTemplate>
DataTemplate>
Grid>
Grid.ColumnDefinitions>
ColumnDefinition/>
/Grid.ColumnDefinitions>
ListBox Background="Gray" ItemsSource="{Binding PlayList, Mode=TwoWay,DisplayMemberPath="Name"}" >
/ListBox>
/Grid>
/DataTemplate>
/controls:TabItem.ContentTemplate>
/controls:TabItem>

the data context initialize in Loaded event (I tried in the constructor too)

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
DataContext = MediaWebControlLogic.Instance.GetContext();
}

this is how I create the object
internal DataContextItems GetContext()
{
MediaPlayerWebControl.DAL.MediaMediator mediator = new MediaPlayerWebControl.DAL.MediaMediator(); DataContextItems context= new DataContextItems();
mediator.GetPlayList(context.PlayListItemCallback);
return context;
}

the list is filled in async call I thought that it may be the problem but simple text property didn't bind too ...
any suggestions?
Thanks
Eran

Upvotes: 0

Views: 182

Answers (2)

Eran
Eran

Reputation: 79

OK I found it... the thing is that the TabItem DataContext wasn't the same as the TabControl DataContex so I couldn't just Use The "Name" property, after binding the TabItem DataContext to the TabControl DataContext the binding worked fine for me

Upvotes: 0

Vladimir Dorokhov
Vladimir Dorokhov

Reputation: 3839

Incorrect syntax: "{Binding PlayList, Mode=TwoWay,DisplayMemberPath="Name"}"

Use:

< ListBox ItemsSource="{Binding PlayList}" DisplayMemberPath="Name"/>

Upvotes: 1

Related Questions