silverlight xaml 到 C#

发布于 2024-10-21 04:25:20 字数 4514 浏览 1 评论 0原文

有人可以给出这个 xamle 代码的 C# 等效代码吗?

<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 动态添加到数据透视表中,对于 Windows Phone 7 应用程序,并在 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);
}

提前谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑轻吟梦一曲 2024-10-28 04:25:20

您必须使用 XamlReader 在手机上创建数据模板(如在 XAML 中)。

请参阅 http://www.windowsphonegeek 上的完整示例和说明.com/tips/wp7-dynamically-generate-datatemplate-in-code

有关动态添加 PivotItems 的示例,请参阅如何动态添加枢轴项并向每个枢轴项添加图像?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文