Silverlight XAML для C # - PullRequest
       6

Silverlight XAML для C #

2 голосов
/ 11 марта 2011

может кто-нибудь дать C # эквивалент этого кода xamle?

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

Это потому, что я хотел бы знать, как динамически добавлять PivotItems в Pivot для приложения Windows Phone 7 со стандартным макетом Pivot, генерируемым в VS 2010. Например, я получил этот код, но не нашел способ добавить объект StackPanel sp в 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);
}

Спасибо заранее

1 Ответ

2 голосов
/ 11 марта 2011

Вы должны использовать XamlReader для создания таблиц данных (как в вашем XAML) на телефоне.

См. Полный пример и объяснение на http://www.windowsphonegeek.com/tips/wp7-dynamically-generating-datatemplate-in-code

Пример динамического добавления PivotItems см. В разделе Как динамически добавлять элемент сводки и добавлять изображение к каждому элементу сводки?

...