WP8のアプリケーションを作成しようとしていますが、データバインディングがどのように機能するかを一生理解できません。私は例を次々と試しましたが、彼らは私とほとんど同じようにやっているようですが、何もうまくいかないようです。基本的に、プロファイルクラスには、プロファイルの名前とアイコンが含まれます。これらのプロファイルのリストを、アイコンの右側に名前を付けて画面に表示したいと思います。
WP8電話エミュレーターでプロジェクトを実行すると、何も表示されません。DataTemplateの要素(つまり、ソースとテキスト)のプロパティを絶対文字列に変更すると、正常に機能します。
MainPage.xaml:
<phone:PhoneApplicationPage ..>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.Resources>
<DataTemplate x:Name="ProfileListTemplate">
<StackPanel Margin="10">
<Image Grid.Column="0" Width="50" Height="50" Source="{Binding ImageSource}" Stretch="Fill"/>
<TextBlock Grid.Column="1" Text="{Binding ProfileName}" Margin="10" HorizontalAlignment="Left" FontSize="36"/>
</StackPanel>
</DataTemplate>
</Grid.Resources>
<phone:LongListSelector x:Name="ProfilesList" Grid.Row="1" VerticalAlignment="Top" FontSize="36" Height="535" Margin="10,0,0,0" ItemTemplate="{StaticResource ProfileListTemplate}"/>
</Grid>
</phone:PhoneApplicationPage>
MainPage.xaml.cs:
namespace Profiles
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
ObservableCollection<Profile> ProfilesCollection = new ObservableCollection<Profile>();
ProfilesCollection.Add(new Profile("Nighttime"));
ProfilesCollection.Add(new Profile("Work"));
ProfilesCollection.Add(new Profile("Home"));
ProfilesList.ItemsSource = ProfilesCollection;
}
}
}
「プロファイル」クラス:
namespace Profiles
{
class Profile
{
public string ProfileName = "";
public string ImageSource = "/Resources/Delete.png";
public Profile(string name)
{
ProfileName = name;
}
}
}