请问我在listpicker控件中显示数据的方式有问题。我实际上从一个Web服务加载,但我后来尝试使用静态数据,但我发现我无法加载我的静态数据。它使我的MVVM出错。我粘贴了下面的代码。
我的模特课
public class ContinentSelect
{
public int ID { get; set; }
public string Name { get; set; }
}
ViewModels
public class ContinentViewModel : INotifyPropertyChanged
{
private ContinentSelect continent;
// public ContinentViewModel() { }
public ContinentViewModel(ContinentSelect continent)
{
this.continent = continent;
}
public string ID
{
get
{
return continent.ID.ToString();
}
set
{
if (value != continent.ID.ToString())
{
continent.ID = int.Parse(value);
NotifyPropertyChanged("ID");
}
}
}
public string Name
{
get
{
return continent.Name;
}
set
{
if (value != continent.Name)
{
continent.Name = value;
NotifyPropertyChanged("Name");
}
}
}
//This would notify the view when there is a direct change in the values of the list items
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class MainContinentViewModel : INotifyPropertyChanged
{
IRegionService regionservice = null;
public MainContinentViewModel()
{
this.Items = new ObservableCollection<ContinentViewModel>();
regionservice = new RegionService();
}
public ObservableCollection<ContinentViewModel> Items { get; private set; }
private string _sampleProperty = "Sample Runtime Property Value";
/// <summary>
/// Sample ViewModel property; this property is used in the view to display its value using a Binding
/// </summary>
/// <returns></returns>
public string SampleProperty
{
get
{
return _sampleProperty;
}
set
{
if (value != _sampleProperty)
{
_sampleProperty = value;
NotifyPropertyChanged("SampleProperty");
}
}
}
/// <summary>
/// Sample property that returns a localized string
/// </summary>
public string LocalizedSampleProperty
{
get
{
return AppResources.SampleProperty;
}
}
public bool IsDataLoaded
{
get;
private set;
}
public void LoadData()
{
// this.CollectContinents();
ContinentSelect continent = new ContinentSelect() { ID = 0, Name = "Jide" };
ContinentViewModel viewmodel = new ContinentViewModel(continent);
Items.Add(viewmodel);
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
然后我的xaml视图如下。我跳过一些代码使其可读且简短
<phone:PhoneApplicationPage
x:Class="JizB2c.UserLocation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DataContext="{d:DesignData SampleData/ContinentSampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
Loaded="UserLocationPage_Loaded">
<toolkit:ListPicker
Grid.Column="0"
Grid.Row="0"
Grid.ColumnSpan="2"
x:Name="listPicker"
Header="Choose Continent">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Orientation="Horizontal">
<TextBlock Text="{Binding ID}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
</phone:PhoneApplicationPage>