As I was trying to learn Data Binding, I found one of the quick start examples not workin. Could you tell me if its wrong or have I gone wrong somewhere?
http://create.msdn.com/en-US/education/quickstarts/Data_Binding_to_Controls#DisplayingItemsWithDataTemplate
The example uses CollectionView Source and ObservableCollection classes to dynamically bind the data to the UI elements. I have used a listpicker instead of a combo box. How ever I find that the the textblocks does not change as the selection in the list picker changes. Do I have to implement the INotifyProperty Changed? The following is my C# code
namespace binding3
{
public partial class MainPage : PhoneApplicationPage
{
public ObservableCollection<Recording> MyMusic = new ObservableCollection<Re开发者_C百科cording>();
// Constructor
public MainPage()
{
InitializeComponent();
MyMusic.Add(new Recording("Chris Sells", "Chris Sells Live", new DateTime(2008, 2, 5)));
MyMusic.Add(new Recording("Luka Abrus", "The Road to Redmond", new DateTime(2007, 4, 3)));
MyMusic.Add(new Recording("Jim Hance", "Best of Jim Hance", new DateTime(2007, 3, 6)));
//listPicker1.DataContext = MyMusic;
//RecordingDetails.DataContext = new CollectionViewSource { Source = MyMusic };
LayoutRoot.DataContext = new CollectionViewSource { Source = MyMusic };
}
public class Recording
{
public Recording() { }
public Recording(string artistName, string cdName, DateTime release)
{
Artist = artistName;
Name = cdName;
ReleaseDate = release;
}
public string Artist { get; set; }
public string Name { get; set; }
public DateTime ReleaseDate { get; set; }
public override string ToString()
{
return Name + " by " + Artist + ", Released: " + ReleaseDate.ToShortDateString();
}
}
and this is the xaml design
<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<toolkit:ListPicker Height="150" HorizontalAlignment="Left" Margin="5" x:Name="listPicker1"
VerticalAlignment="Top" Width="400" ItemsSource="{Binding}" FontSize="18">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Text="Artist:" Margin="2"/>
<TextBlock Text="{Binding Artist}" Margin="2"/>
<TextBlock Text="CD:" Margin="10,2,0,2"/>
<TextBlock Text="{Binding Name}" Margin="2"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
<StackPanel x:Name="RecordingDetails">
<TextBlock Text="{Binding Artist}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding Name}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding ReleaseDate}" Margin="5,0,0,0"/>
</StackPanel>
</StackPanel>
Thank You,
Alfah
The code works when you modify the xaml like this
<StackPanel x:Name="RecordingDetails">
<TextBlock Text="{Binding SelectedItem.Artist, ElementName= listPicker1}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding SelectedItem.Name, ElementName= listPicker1}" Margin="5,0,0,0"/>
<TextBlock Text="{Binding SelectedItem.ReleaseDate,ElementName= listPicker1}" Margin="5,0,0,0"/>
</StackPanel>
You need to bind the selected item of the listpicker to the textblock so that the textblock is updated as the selection is changed. Dont know if there is a better way to do it. This solves the problem anyway.
Alfah
精彩评论