Silverlight学习笔记九ListBox控件

Silverlight ListBox控件示例
本文介绍了一个使用Silverlight ListBox控件的示例程序,展示了如何实现列表项的拖拽功能及根据不同的属性显示列表项内容。

ListBox是SilverLight列表控件

2010071317442197.png

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的显示项,通过改变这个属性,来改变所显示对象的属性。

转载于:https://www.cnblogs.com/salam/archive/2010/07/13/1776662.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值