0
votes

I have edited the question to include more info based on the feedback.

In my SettingsPage.xaml I have

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

I am trying to bind the ListPicker DataTemplate to the following XAML placed in the App.xaml page

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

The SettingsPage.xaml.cs defined Country as public static string array:

namespace myspace
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        public static String[] Country = {
                            "Afghanistan",
                            "Åland Islands",
                            "Albania",
                            "Algeria",
                            "American Samoa",
                            "Andorra",
                            "Angola"}
                            ......;

Also on the SettingsPage.xaml.cs I have defined the datacontext to another object.

    public SettingsPage()
    {
        InitializeComponent();
         this.lpkCountry.SelectionChanged += new  SelectionChangedEventHandler(lpkCountry_SelectionChanged);
        this.lpkCountry.ItemsSource = Country;
        settings = new AppSettings();
        this.DataContext = settings;
    }

But on runtime when I go to SettingsPage I get many errors of this kind

System.Windows.Data Error: BindingExpression path error: 'Country' property not found on 'Afghanistan' 'System.String' (HashCode=-2039466552). BindingExpression: Path='Country' DataItem='Afghanistan' (HashCode=-2039466552); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..

I understand that there's conflict between target element and target property, so how to fix this?

1

1 Answers

1
votes

One obvious mistake is RelativeSource={RelativeSource Self}. This means you try to bind to the very same object, like TextBox or ListPicker. Runtime is absolutely right in this case: 'lpkItemTemplate' property not found on 'Microsoft.Phone.Controls.ListPicker'

I guess something like Source={StaticResource lpkItemTemplate} would help, if you define your data template somewhere in App.xaml, in <Application.Resources> section.

EDIT: after you added more code. Your items source is an array of strings, so in data template you should use the following binding:

<TextBox Text={Binding} />