WP7 Binds UserControl Using MVVM

Possible duplicate:
Linking between Usercontrol with a list and parental control (MVVM)

I am trying to bind a UserControl property to a property on the viewmodel main page. The code is as follows:

UserControl xaml:

<UserControl x:Class="myUserControl"  ....>
     <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" >
         <ListBox Name="myListBox" ItemsSource="{Binding Path=myItemsSource}"/>
    </Grid>
</UserControl>

codebehind looks like this:

public partial class myUserControl : UserControl
{
     public static DependencyProperty ItemsSourceProperty =
                   DependencyProperty.Register("myItemsSource", 
                   typeof(IEnumerable), typeof(myUserControl), null);

     public IEnumerable myItemsSource
     {
        get
        {
            return (IEnumerable)GetValue(ItemsSourceProperty);
        }
        set
        {
            SetValue(ItemsSourceProperty, value);
        }
    }
}

UC is used on the main page:

<phone:PhoneApplicationPage DataContext="{Binding myViewModel, Source={StaticResource Locator}}"  ....>
   <Grid x:Name="ContentPanel">
        <uc:myUserControl x:Name="ucList" myItemsSource="{Binding Path=DataList}"/>
   </Grid>
</phone:PhoneApplicationPage>

and viewModel for the main page are as follows:

public class myViewModel : ViewModelBase
{
    public ObservableCollection<myObject> DataList
    {
        get
        {
            return _datalist;
        }
        set
        {
            if (_dataList != value)
            {
               _dataList = value;
               RaisePropertyChanged("DataList");
            }
        }
    }
}

But when the DataList property is set, the uc list is not populated. What am I missing?

+3
source share
1 answer

, , , . ItemsSource, , .

? , :

<UserControl x:Class="myUsercontrol"  ....>     
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" >         
    <ListBox Name="myListBox" ItemsSource="{Binding Path=myItemsSource}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding SomeListItemProperty}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

EDIT: MVVM Light? , {StaticResource = Locator} . .

, , , UserControl . UserControl, . UserControl, , ViewModel.

0

All Articles