Silverlight学习笔记九ListBox控件

ListBox是SilverLight列表控件

1.ListBoxDemo.xaml

<UserControl x:Class="Silverlight.Common.View.ListBoxDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:toolKit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="120" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0" FlowDirection="LeftToRight">
            <CheckBox Content="是否允许拖动" x:Name="IsdragDrop" IsChecked="True" Click="CheckBox_Click"></CheckBox>
            <TextBox x:Name="txtSelectValue" Text="SelectValue" Margin="3"></TextBox>
            <TextBox x:Name="txtSelectItem" Text="SelectItem" Margin="3"></TextBox>
            <TextBlock Text="显示项:"></TextBlock>
                <ComboBox HorizontalAlignment="Left" Width="auto" SelectedItem="{Binding DisplayMemberPath, ElementName=listBox, Mode=TwoWay}" Margin="4" SelectedIndex="2">
                <sys:String>Name</sys:String>
                <sys:String>IsEnabled</sys:String>
                <sys:String>UserID</sys:String>
            </ComboBox>
   
        </StackPanel>
        <toolKit:ListBoxDragDropTarget  x:Name="dragDrop1" AllowDrop="True" Grid.Column="1">
            <ListBox Height="200" Width="200"  x:Name="listBox" ItemsSource="{Binding}" DisplayMemberPath="Name">
              
            </ListBox>
        </toolKit:ListBoxDragDropTarget>
        <toolKit:ListBoxDragDropTarget  x:Name="dragDrop2" AllowDrop="True" Grid.Column="2">
            <ListBox Height="200" Width="200" DisplayMemberPath="Name">
             
            </ListBox>
        </toolKit:ListBoxDragDropTarget>
    </Grid>
</UserControl>

 

2.ListBoxDemo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Silverlight.Common.Core;

namespace Silverlight.Common.View
{
    public partial class ListBoxDemo : UserControl
    {
        public ListBoxDemo()
        {
            InitializeComponent();
            this.DataContext = UserList.GetUserList();
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (!(bool)this.IsdragDrop.IsChecked)
            {
                this.dragDrop1.AllowDrop = false;
                this.dragDrop2.AllowDrop = false;
             
            }

            else
            {
                this.dragDrop1.AllowDrop = true;
                this.dragDrop2.AllowDrop = true;
            }

        }

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.listBox.SelectedValue!=null)
            {
             User user=this.listBox.SelectedValue as User;
             this.txtSelectValue.Text = user.Name;
            }

            if (this.listBox.SelectedItem != null)
            {
                User user = this.listBox.SelectedItem as User;
                this.txtSelectItem.Text = user.Name;
            }
        }
    }
}

 

 注:DisplayMemberPath是ListBox的显示项,通过改变这个属性,来改变所显示对象的属性。

posted @ 2010-07-13 17:48  ForrestWoo  阅读(2687)  评论(4编辑  收藏  举报