zoukankan      html  css  js  c++  java
  • 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的显示项,通过改变这个属性,来改变所显示对象的属性。

  • 相关阅读:
    ASP.NET MVC深入浅出(被替换)
    第十七节: EF的CodeFirst模式的四种初始化策略和通过Migration进行数据的迁移
    第十六节: EF的CodeFirst模式通过Fluent API修改默认协定
    第十五节: EF的CodeFirst模式通过DataAnnotations修改默认协定
    第十四节: EF的三种模式(四) 之 原生正宗的 CodeFirst模式的默认约定
    第十三节: EF的三种模式(三) 之 来自数据库的CodeFirst模式
    C#读写记事本(txt)文件
    JSP生成验证码
    SQLServer创建用户、数据库、表、约束、存储过程、视图
    windows7 asp.net发布IIS 拒绝访问 解决方法
  • 原文地址:https://www.cnblogs.com/salam/p/1776662.html
Copyright © 2011-2022 走看看