一起學Windows Phone7開發(十三.八 ListBox控制項)

來源:互聯網
上載者:User

      在Phone7中去掉了listview控制項,就只有這個listbox控制項,不過這個控制項功能非常強大,完全可以實現listview的功能。 因為這個控制項也相當於一個容器,可以通過ListItem來組合多個控制項而得到不同功能的list。

Xaml:

<ListBox Grid.Row="1" Height="567" HorizontalAlignment="Left" Margin="12,53,0,0" Name="listBox1" VerticalAlignment="Top" Width="460">

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <StackPanel Orientation="Horizontal">

                        <Image Source="{Binding ImgSource}" Width="130" Height="130"/>

                        <TextBlock Text="{Binding Name}" Foreground="Yellow" FontSize="25"/>

                    </StackPanel>

                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox>

//{Binding ImgSource}:動態綁定圖片資源。

//{Binding Name}:動態綁定text文字。

原始碼:

ImageList item1 = new ImageList();

    item1.ImgSource = new BitmapImage(new Uri("Images/Chrysanthemum.jpg", UriKind.Relative));

    item1.Name = "Chrysanthemum.jpg";

    list.Add(item1);

 

     ImageList item2 = new ImageList();

     item2.ImgSource = new BitmapImage(new Uri("Images/Desert.jpg", UriKind.Relative));

     item2.Name = "Desert.jpg";

     list.Add(item2);

 

    ImageList item3 = new ImageList();

    item3.ImgSource = new BitmapImage(new Uri("Images/Hydrangeas.jpg", UriKind.Relative));

    item3.Name = "Hydrangeas.jpg";

    list.Add(item3);

 

    listBox1.ItemsSource = list;

 

 

<ListBox Height="605" HorizontalAlignment="Left" Margin="10,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="460" >

      <ListBox.ItemTemplate>

           <DataTemplate>

              <StackPanel Orientation="Horizontal">

                 <Button  Content="{Binding ColorName}" Click="Button_Click"/>

                 <TextBlock  x:Name="tb" Text="{Binding ColorName}" FontSize="35"/>

               </StackPanel>

           </DataTemplate>

       </ListBox.ItemTemplate>

 </ListBox>

 

void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

            ColorItem color = new ColorItem();

            color.ColorName = "Red";

            color.ColorValue = Colors.Red;

            list.Add(color);

 

            ColorItem color1 = new ColorItem();

            color1.ColorName = "Green";

            color1.ColorValue = Colors.Green;

            list.Add(color1);

 

            ColorItem color2 = new ColorItem();

            color2.ColorName = "Blue";

            color2.ColorValue = Colors.Blue;

            list.Add(color2);

 

            this.listBox1.ItemsSource = list;

        }

 

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            string button = (sender as Button).Content as string;

          

             int index = 0;

             string color = string.Empty;

             for(index=0; index<list.Count; index++)

             {

                if(button == list[index].ColorName)

                {

                    color = list[index].ColorName;

                    break;

                }

 

             }

            

             ListBoxItem lbi = this.listBox1.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;

             TextBlock tb = FindFirstVisualChild<TextBlock>(lbi, "tb");

             tb.Foreground = new SolidColorBrush(list[index].ColorValue);

             

            }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.