zoukankan      html  css  js  c++  java
  • Building a ListBox with custom content in Silverlight 4.0

    Let's take the same data that displayed poker starting hands from the previous exercise and see what type of cool Listbox you can build with it.

    1. Start out by creating a new Silverlight applicatoin called ListBoxCustom and allow VS to create a hosting web site.

    2. You will use the same class that you build in the earlier DataGrid exercise. Right-click the Silverlight project, choose Add existing Item, and browe to StartingHands.cs to add that class to the project.

    3. When you add the existing StartingHands.cs class, it is in a different namespace than your current project. you can reference that namespace by adding a using statement at the top of your silverlight application, or you can just change the namespace, as follows:

    namespace ListBoxCustom

    {

      public class StartingHands

      {

         public string Nickname { get; set; }
              public string Notes { get; set; }
              public string Card1 { get; set; }
              public string Card2 { get; set; }

        ...

      }

    }

    4. Next, you need to define the ListBox's ItemTemplate. The ItemTemplate will contain a horizontal-oriented StackPanel including the grid to display the two cards. It will also include a nested vertical-oriented StackPanel that will contain two TextBlock controls to display the Nickname and Notes data. here is the code:

    <ListBox Margin="10" x:Name="list">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <!--<StackPanel Margin="5" Orientation="Vertical">
                            <TextBlock FontSize="17" FontWeight="Bold" Text="{Binding Nickname}" />
                            <StackPanel Margin="5,0,0,0" Orientation="Horizontal">
                                <TextBlock Text="Age: " />
                                <TextBlock Text="{Binding Age}" />
                                <TextBlock Text=", Male: " />
                                <TextBlock Text="{Binding Male}" />
                            </StackPanel>
                        </StackPanel>-->
                        <StackPanel Margin="5" Orientation="Horizontal">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition  />
                                    <ColumnDefinition  />
                                </Grid.ColumnDefinitions>
                                <Border Margin="2" CornerRadius="4" BorderBrush="Black" BorderThickness="1" />
                                <Rectangle Margin="4" Fill="White" Grid.Column="0" Width="20" />
                                <Border Margin="2" CornerRadius="4" BorderBrush="Black" BorderThickness="1" Grid.Column="1" />
                                <Rectangle Margin="4" Fill="White" Grid.Column="1" Width="20" />
                                <TextBlock Text="{Binding Card1}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0"  />
                                <TextBlock Text="{Binding Card2}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1" />
                               
                            </Grid>
                            <StackPanel Grid.Column="2" Orientation="Vertical">
                                <TextBlock Text="{Binding Nickname}" FontWeight="Bold" />
                                <TextBlock Text="{Binding Notes}" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

    5. The only thing left to do is to wire up the ListBox to the data source. To do this, navigate to the page.xmal.cs code behind, and add an event handler for the Page Load event. Then, within that Loaded event handler, add the following code to set the ListBox's ItemsSource to the return value form the StartingHands.GetHands() method, as you did earlier in the DataGrid example.

    public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            }

            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                this.list.ItemsSource = StartingHands.GetHands();

        }

    6. Run the application. If all goes well, you will see the listbox example.

  • 相关阅读:
    关闭 Window 之后,无法设置 Visibility,也无法调用 Show、ShowDialogor 或 WindowInteropHelper.EnsureHandle。
    WPF中的逻辑树和可视化树
    StopWatch
    【转】Oracle imp 总是不停地重复闪烁
    Ubuntu下借助URLOS实现快速安装DzzOffice企业办公套件
    linux下使用URLOS搭建nextcloud私有云盘系统
    在Debian下利用URLOS快速安装SqlServer2017
    使用URLOS 五分钟安装rTorrent (轻量级优秀BT/PT客户端)
    URLOS开发基础教程——docker容器的使用方法
    使用URLOS在linux系统中极速部署NFS共享存储服务
  • 原文地址:https://www.cnblogs.com/jerrychenfly/p/2139518.html
Copyright © 2011-2022 走看看