Silverlight数据绑定MVVM
我在Silverlight中设置数据绑定时遇到一些问题 我尝试使用MVVM方法,并找到了一些很好的示例,因此我创建了我的视图和视图模型,我创建了一些我将用于包含数据的类,以及一个用于填充类的类 首先,我的ViewModel看起来像:Silverlight数据绑定MVVM,silverlight,xaml,binding,Silverlight,Xaml,Binding,我在Silverlight中设置数据绑定时遇到一些问题 我尝试使用MVVM方法,并找到了一些很好的示例,因此我创建了我的视图和视图模型,我创建了一些我将用于包含数据的类,以及一个用于填充类的类 首先,我的ViewModel看起来像: public class MainPageVM : INotifyPropertyChanged { ObservableCollection<Item> Items; public MainPageVM()
public class MainPageVM : INotifyPropertyChanged
{
ObservableCollection<Item> Items;
public MainPageVM()
{
InitializeItems InitItems = new InitializeItems();
InitItems.GenerateItemList(out Items);
RaiseProertyChanged("Items");
}
public string test = "Binding Test";
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
private void RaiseProertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
public类MainPageVM:INotifyPropertyChanged
{
可观察的收集项目;
公共MainPageVM()
{
InitializeItems InitItems=新的InitializeItems();
InitItems.GenerateItemList(out项);
提高预付款变更(“项目”);
}
公共字符串test=“绑定测试”;
公共事件System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
私有void RaiseProertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
在我看来,我有:
<UserControl.Resources>
<viewmodel:MainPageVM x:Key="ViewModel" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource ViewModel}">
<StackPanel>
<TextBlock Text="{Binding test}"/>
<ListBox ItemsSource="{Binding Items}"
Width="200"
Height="200">
<ListBoxItem Width="190" Height="20">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ItemName}"/>
<TextBlock Text="-"/>
<TextBlock Text="{Binding ItemID}"/>
</StackPanel>
</ListBoxItem>
</ListBox>
</StackPanel>
</Grid>
我添加了断点,我知道我尝试绑定到的ObservableCollection正在填充,但没有绑定任何内容,在错误窗口中,我刚刚获取的xxx属性在MainPageVM中不存在
这里的任何建议都会很好,因为我对可能发生的事情有点不知所措,这是我的第一个silverlight应用程序
谢谢物品必须是公共财产。你的测试场也一样。在Silverlight中,只能绑定到公共属性
此外,通常在这些属性的Setter中引发属性更改事件。这将告诉Silverlight运行时使用该属性的新值刷新绑定到该属性的控件。谢谢,我认为这将允许我绑定到公共变量而不是属性。为什么有人对此投了否决票?