gpt4 book ai didi

windows-phone-7 - WP7 使用 MVVM 绑定(bind) UserControl

转载 作者:行者123 更新时间:2023-12-04 16:06:12 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Binding between Usercontrol with listbox and parent control (MVVM)

我正在尝试将 UserControl 绑定(bind)到主页 View 模型上的属性代码如下所示:

用户控件 xaml:

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

代码隐藏看起来像这样:

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在主页面中是这样使用的:

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

但是当设置了 DataList 属性时,uc 列表不会被填充。我错过了什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com