我已经根据反馈编辑了问题以包含更多信息。
在我的 SettingsPage.xaml 我有
<toolkit:ListPicker x:Name="lpkCountry"
ItemTemplate="{Binding Source={StaticResource lpkItemTemplate}}"
FullModeItemTemplate="{Binding Source={StaticResource lpkFullItemTemplate}}"
Header="your country" CacheMode="BitmapCache"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="280" Height="82" />
我正在尝试将 ListPicker DataTemplate 绑定到放置在 App.xaml 页面中的以下 XAML
<Application.Resources>
<DataTemplate x:Name="lpkItemTemplate">
<TextBlock Text="{Binding Country}" />
</DataTemplate>
<DataTemplate x:Name="lpkFullItemTemplate">
<TextBlock Margin="16 0 0 0" Text="{Binding Country}" />
</DataTemplate>
</Application.Resources>
SettingsPage.xaml.cs 将 Country 定义为公共静态字符串数组:
namespace myspace
{
public partial class SettingsPage : PhoneApplicationPage
{
public static String[] Country = {
"Afghanistan",
"Åland Islands",
"Albania",
"Algeria",
"American Samoa",
"Andorra",
"Angola"}
......;
同样在 SettingsPage.xaml.cs 上,我已将 datacontext 定义为另一个对象。
public SettingsPage()
{
InitializeComponent();
this.lpkCountry.SelectionChanged += new SelectionChangedEventHandler(lpkCountry_SelectionChanged);
this.lpkCountry.ItemsSource = Country;
settings = new AppSettings();
this.DataContext = settings;
}
但是在运行时,当我转到 SettingsPage 时,我会遇到很多此类错误
System.Windows.Data 错误:BindingExpression 路径错误:在“阿富汗”“System.String”上找不到“Country”属性(HashCode=-2039466552)。BindingExpression: Path='Country' DataItem='Afghanistan' (HashCode=-2039466552); 目标元素是'System.Windows.Controls.TextBlock'(名称='');目标属性是“文本”(类型“System.String”)..
我知道目标元素和目标属性之间存在冲突,那么如何解决这个问题?