can someone give the C# equivalent of this xamle code?
<ListBox Width="400" Margin="10"
ItemsSource="{Binding Source={StaticResource myTodoList}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=TaskName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
That's because I would like to know how to dynamically add PivotItems into a Pivot, for a windows phone 7 app, with the default Pivot layout generate in VS 2010. For instance, I got this code, but I didn't find a way to add the StackPanel object sp, to ListBox:
// epgXml is an XElement object, wich has channel and programmes tags
// it's an app about a tv guide, where the user clicks on a channel and gets a list of programmes for that channe.
// in th xaml code, I have a grid with a pivot object in it:
<Grid x:Name="LayoutRoot" Background="Transparent">
<controls:Pivot x:Name="MainPivot" ItemsSource="{Binding PivotItems}" Title="ZON EPG">
</controls:Pivot>
</Grid>
// In MainViewModel.cs I have this code:
public void client_GetEPGCompleted(Object sender, GetEPGCompletedEventArgs e)
{
DataTemplate dt = new DataTemplate();
epgXml = e.Result.Output.EPGXml;
if (epgXml != null)
{
//Channels = new List<string>();
// parse the xml and show channels and programmes
var channels = from c in epgXml.Elements("channel")
select c;
foreach (XElement el in channels)
{
PivotItem pi = new PivotItem();
pi.Header = el.Elements("display-name").FirstOrDefault().Value;
ListBox lb = new ListBox();
lb.Margin = new Thickness(0, 0, -12, 0);
//ItemsControl ic = new ItemsControl();
//ic.ItemTemplate = dt;
lb.ItemTemplate = dt;
StackPanel sp = new StackPanel();
sp.Margin = new Thickness(0, 0, 0, 17);
sp.Width = 432;
TextBlock tb = new TextBlock();
Binding binding = new Binding("ProgTitle");
binding.Mode = BindingMode.TwoWay;
tb.SetBinding(TextBlock.TextProperty, binding);
tb.Margin = new Thickness(12, 0, 0, 0);
tb.TextWrapping = TextWrapping.NoWrap;
tb.Style = Application.Current.Resources["PhoneTextExtraLargeStyle"] as Style;
sp.Children.Add(tb);
tb = new TextBlock();
binding = new Binding("ProgStart");
binding.Mode = BindingMode.TwoWay;
tb.SetBinding(TextBlock.TextProperty, binding);
tb.Margin = new Thickness(12, -6, 0, 0);
tb.TextWrapping = TextWrapping.NoWrap;
tb.Style = Application.Current.Resources开发者_运维知识库["PhoneTextSubtleStyle"] as Style;
sp.Children.Add(tb);
tb = new TextBlock();
binding = new Binding("Duration");
binding.Mode = BindingMode.TwoWay;
tb.SetBinding(TextBlock.TextProperty, binding);
tb.Margin = new Thickness(12, 0, 0, 0);
tb.TextWrapping = TextWrapping.NoWrap;
tb.Style = Application.Current.Resources["PhoneTextSubtleStyle"] as Style;
sp.Children.Add(tb);
// get programmes foreach channel
var progrs = from p in epgXml.Elements("programme")
where p.Attributes("channel").FirstOrDefault().Value == el.Attributes("id").FirstOrDefault().Value
select new ItemViewModel()
{
ProgTitle = p.Elements("title").FirstOrDefault().Value,
ProgStart = p.Attributes("start").FirstOrDefault().Value,
Duration = p.Elements("length").FirstOrDefault().Value
};
foreach (ItemViewModel item in progrs)
{
this.Items.Add(item);
}
// create new instance - this holds all the programms for each channel
this.Items = new ObservableCollection<ItemViewModel>();
PivotItems.Add(pi);
}
Thx in advance
You have to use XamlReader
to create datatemplates (as in your XAML) on the phone.
See a full example and explanation at http://www.windowsphonegeek.com/tips/wp7-dynamically-generating-datatemplate-in-code
For an example off dynamically adding PivotItems seee How to dynamically add pivot item and add Image to each pivot item?
精彩评论