吴佳鑫的个人专栏

当日事当日毕,没有任何借口

导航

sivlerlight 之 ListBox各种数据源绑定demo

前台XAM;:

<UserControl
    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:SampleData="clr-namespace:Expression.Blend.SampleData.SampleDataSource"
    mc:Ignorable="d"
    x:Name="userControl" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" xmlns:System_Collections="clr-namespace:System.Collections;assembly=mscorlib"
    x:Class="Class2Samples.ListBox"
    d:DesignWidth="640" d:DesignHeight="480">
    <UserControl.Resources>
        <SampleData:SampleDataSource x:Key="SampleDataSource"/>
        <DataTemplate x:Key="DataTemplate">
            <Grid Width="360">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="5*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Height="20" Source="{Binding Path=Content}"/>
                <TextBlock Margin="10,0,10,0" Grid.Column="1" Text="{Binding Path=Title}" TextWrapping="Wrap" d:LayoutOverrides="Width"/>
                <RadioButton Grid.Column="2" IsChecked="{Binding Path=Status}" d:LayoutOverrides="Height"/>
            </Grid>
        </DataTemplate>
        <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Padding" Value="3"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="TabNavigation" Value="Local"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid Background="{TemplateBinding Background}" ToolTipService.ToolTip="{Binding Path=Amount}">
                            <vsm:VisualStateManager.VisualStateGroups>
                                <vsm:VisualStateGroup x:Name="CommonStates">
                                    <vsm:VisualState x:Name="Normal"/>
                                    <vsm:VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="fillColor" Storyboard.TargetProperty="Opacity">
                                                <SplineDoubleKeyFrame KeyTime="0" Value=".35"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </vsm:VisualState>
                                    <vsm:VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Opacity">
                                                <SplineDoubleKeyFrame KeyTime="0" Value=".55"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </vsm:VisualState>
                                </vsm:VisualStateGroup>
                                <vsm:VisualStateGroup x:Name="SelectionStates">
                                    <vsm:VisualState x:Name="Unselected"/>
                                    <vsm:VisualState x:Name="Selected">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="fillColor2" Storyboard.TargetProperty="Opacity">
                                                <SplineDoubleKeyFrame KeyTime="0" Value=".75"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </vsm:VisualState>
                                </vsm:VisualStateGroup>
                                <vsm:VisualStateGroup x:Name="FocusStates">
                                    <vsm:VisualState x:Name="Focused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)">
                                                <EasingColorKeyFrame KeyTime="00:00:00" Value="#FFD16D6D"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </vsm:VisualState>
                                    <vsm:VisualState x:Name="Unfocused"/>
                                </vsm:VisualStateGroup>
                            </vsm:VisualStateManager.VisualStateGroups>
                            <Rectangle x:Name="fillColor" Fill="#FFBADDE9" RadiusX="1" RadiusY="1" IsHitTestVisible="False" Opacity="0"/>
                            <Rectangle x:Name="fillColor2" Fill="#FFBADDE9" RadiusX="1" RadiusY="1" IsHitTestVisible="False" Opacity="0"/>
                            <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                            <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1" StrokeThickness="1" RadiusX="1" RadiusY="1" Visibility="Collapsed"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <DataTemplate x:Key="DataTemplate1">
            <Grid MinWidth="100">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="2*"/>
                </Grid.ColumnDefinitions>
                <TextBlock MinWidth="20" Text="{Binding Path=Amount}" TextWrapping="Wrap"/>
                <TextBlock MinWidth="20" Grid.Column="1" Text="{Binding Path=Name}" TextWrapping="Wrap"/>
            </Grid>
        </DataTemplate>
        
        <ListBox x:Key="ArrayListDataSource">        
            <SampleData:Item Title="Praesent inceptos dictumst facilisi" Status="False" />
            <SampleData:Item Title="Praesent pulvinar torquent" Status="False" />
            <SampleData:Item Title="Inceptos facilisi" Status="True" />
            <SampleData:Item Title="Nascetur pharetra" Status="True" />
            <SampleData:Item Title="Eleifend praesent" Status="False" />
            <SampleData:Item Title="Facilisi sagittis maecenas" Status="True" />
            <SampleData:Item Title="Pharetra" Status="True" />
            <SampleData:Item Title="Suscipit senectus inceptos" Status="True" />
            <SampleData:Item Title="Lobortis" Status="False" />
            <SampleData:Item Title="Ultrices accumsan eleifend" Status="True" />
        </ListBox>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot">
        <ListBox ItemsSource="{Binding Items}" ItemTemplate="{StaticResource DataTemplate}" DataContext="{Binding Source={StaticResource ArrayListDataSource}}" Margin="0,50,272,66" VerticalAlignment="Stretch"/>
        <ListBox ItemsSource="{Binding Collection, Mode=OneWay}" ItemTemplate="{StaticResource DataTemplate}" d:DataContext="{Binding Source={StaticResource SampleDataSource}}" Margin="113,8,159,108" VerticalAlignment="Stretch"/>
        <ListBox Height="182" HorizontalAlignment="Right" Margin="0,50,8,0" VerticalAlignment="Top" Width="260" DisplayMemberPath="Name" ItemsSource="{Binding MyDatas, ElementName=userControl, Mode=OneWay}" ItemContainerStyle="{StaticResource ListBoxItemStyle}"/>
        <ListBox HorizontalAlignment="Right" Margin="0,236,8,66" Width="260" ItemsSource="{Binding MyDatas, ElementName=userControl, Mode=OneWay}" ItemContainerStyle="{StaticResource ListBoxItemStyle}" ItemTemplate="{StaticResource DataTemplate1}"/>
    </Grid>
</UserControl>

 

后台CS:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Expression.Blend.SampleData.SampleDataSource;
using System.Collections.ObjectModel;

namespace Class2Samples
{
    public partial class ListBox : UserControl
    {
        #region Initialize
        public ListBox()
        {
            // Required to initialize variables
            InitializeComponent();
            this.PrepareDatas();
        }

        private void PrepareDatas()
        {
            this.MyDatas = new ObservableCollection<MyDataType>();
            for (int i = 0; i < 10; i++)
            {
                this.MyDatas.Add(new MyDataType("name" + i, i * 5));
            }
        }
        #endregion

        #region DependencyProperties
        public ObservableCollection<MyDataType> MyDatas
        {
            get { return (ObservableCollection<MyDataType>)GetValue(MyDatasProperty); }
            set { SetValue(MyDatasProperty, value); }
        }

        public static readonly DependencyProperty MyDatasProperty =
            DependencyProperty.Register("MyDatas", typeof(ObservableCollection<MyDataType>), typeof(ListBox), new PropertyMetadata(null));
        #endregion

        #region Internal Classes
        public class MyDataType
        {
            public MyDataType(string name, int amount)
            {
                this.Name = name;
                this.Amount = amount;
            }

            public string Name
            {
                get;
                set;
            }

            public int Amount
            {
                get;
                set;
            }
        }
        #endregion
    }
}

 

 

posted on 2012-07-25 00:17  _eagle  阅读(537)  评论(0编辑  收藏  举报