Windows phone 7 使用MVVM绑定WP7用户控件

Windows phone 7 使用MVVM绑定WP7用户控件,windows-phone-7,mvvm,user-controls,Windows Phone 7,Mvvm,User Controls,可能重复: 我正在尝试将UserControl绑定到主页的viewmodel上的属性 代码如下所示: public partial class myUserControl : UserControl { public static DependencyProperty ItemsSourceProperty = DependencyProperty.Register("myItemsSource", typ

可能重复:

我正在尝试将UserControl绑定到主页的viewmodel上的属性 代码如下所示:

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);
        }
    }
}
public class myViewModel : ViewModelBase
{
    public ObservableCollection<myObject> DataList
    {
        get
        {
            return _datalist;
        }
        set
        {
            if (_dataList != value)
            {
               _dataList = value;
               RaisePropertyChanged("DataList");
            }
        }
    }
}
UserControl xaml:

<UserControl x:Class="myUserControl"  ....>
     <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" >
         <ListBox Name="myListBox" ItemsSource="{Binding Path=myItemsSource}"/>
    </Grid>
</UserControl>
UC在主页中的使用方式如下:

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

主页的viewModel如下所示:

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);
        }
    }
}
public class myViewModel : ViewModelBase
{
    public ObservableCollection<myObject> DataList
    {
        get
        {
            return _datalist;
        }
        set
        {
            if (_dataList != value)
            {
               _dataList = value;
               RaisePropertyChanged("DataList");
            }
        }
    }
}
公共类myViewModel:ViewModelBase { 公共可观察收集数据列表 { 得到 { 返回数据列表; } 设置 { 如果(_dataList!=值) { _数据列表=值; RaisePropertyChanged(“数据列表”); } } } } 但是,当设置DataList属性时,不会填充中的uc列表。
我错过了什么?

时间还早,我还没有喝咖啡,但在我看来,您可能需要一个列表项的数据模板。您已经定义了
itemsource
,但没有告诉控件如何呈现项目本身

你的名单上有什么?尝试将数据模板绑定到列表项属性之一,如下所示:

<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>

编辑:您是否偶然使用MVVM Light?我看到您在一条评论中提到了
{StaticResource=Locator}
。知道这一点会很有帮助


您可以尝试的另一件事是检查
UserControl
是否位于自己的命名空间中。如果您将它放在
UserControl
文件夹中,它可能是。当
UserControl
ViewModel

不在同一名称空间时,我在绑定
UserControl时遇到了一些问题。将其添加到主页面可以很好地工作,而无需设置数据模板,并且对UC没有帮助。是的,我使用的是MVVM Light,是的,UC在另一个NS中。我会试着把它移过去。如果你仍在试图弄清楚这一点,请发布一个链接到一个重复该问题的示例项目。我没有开始构建自己的应用程序,但我不知道你是如何构建应用程序的。