wpf 学习篇之 ListView 以及 gridview

今天下午,在做奥运会保障系统的时候,用到了ListView ,因此,在这个地方记录下一些代码...
没什么内容介绍,一看代码就能够明白了..

xaml文件设置
<ListView Name="BscAndMscListView" MaxHeight="200" MaxWidth="250" VerticalAlignment="Top"  Opacity="0.8" ItemsSource="{Binding}">
                    <ListView.View>
                        <GridView AllowsColumnReorder="true" >

                        </GridView>
                    </ListView.View>
                </ListView>

后台代码:

 GridView tempGridView = (GridView)this.BscAndMscListView.View;

            foreach (DataColumn col in resultTable.Columns)
            {
                GridViewColumn gvc = new GridViewColumn();
                gvc.Header = col.ColumnName;
                Binding binding = new Binding();
                binding.Path = new PropertyPath(col.ColumnName);
                gvc.DisplayMemberBinding = binding;
                tempGridView.Columns.Add(gvc);
            }

            this.BscAndMscListView.DataContext = resultTable;

这个是绑定表格的.
还有绑定实现了IEnumrable<t>的对象...(没这样的代码..)

似乎,还可以设置GridView的列的模板,可以加入一些控件等...
(以下代码是从网上摘抄的)

            GridViewColumn gvcName = new GridViewColumn();
gvcName.Header = "Name";
DataTemplate nameTemplate = new DataTemplate();
FrameworkElementFactory nameFactory = new FrameworkElementFactory(typeof(ContactPropertyLabel));
Binding binding = new Binding("Name");
binding.Mode = BindingMode.TwoWay;
nameFactory.SetBinding(ContentProperty, binding);
nameTemplate.VisualTree = nameFactory;
gvcName.CellTemplate = nameTemplate;
gvContactList.Columns.Add(gvcName);


这段代码可以给一个ListView的一列做双向绑定,并且指定这一行的渲染控件。

比Windows Forms更强的Binding

public class Contact : DependencyObject
{
public static DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof (string), typeof (Contact));
public string Name
{
get { return (string) GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
}

DependencyObject + DependencyProperty使得属性设置可以自动触发ValueChanged事件,从而让Binding进行更新。



同时,,也用到了ScrollViewer ,以及动画等.也一并记录吧..

前台:

        <ScrollViewer Name="listScroll" HorizontalAlignment="Left"  VerticalAlignment="Top" Margin="0,30,0,0" Width="10"
                      VerticalScrollBarVisibility="Auto" MaxHeight="700">
            <!--数据列表-->
            <ListView Name="tkgpListView" MaxHeight="700" VerticalAlignment="Top">
                <ListView.View>
                    <GridView AllowsColumnReorder="true" >


                    </GridView>

                </ListView.View>
            </ListView>
        </ScrollViewer>

后台:
  listScroll.MouseEnter += new MouseEventHandler(listScroll_MouseEnter);
                listScroll.MouseLeave += new MouseEventHandler(listScroll_MouseLeave);



        private void listScroll_MouseEnter(object sender, MouseEventArgs e)
        {
          
  GridView tkgpGridView = tkgpListView.View as GridView;
            if (tkgpGridView == null || tkgpGridView.Columns.Count < 1)
                return;
            int iWidth = 0;
            foreach (GridViewColumn col in tkgpGridView.Columns)
            {
                iWidth += (int)col.ActualWidth;
            }

            DoubleAnimation myDoubleAnimationWidth = new DoubleAnimation();
            myDoubleAnimationWidth.To = iWidth + 40;
            myDoubleAnimationWidth.Duration = new Duration(TimeSpan.FromSeconds(((App)Application.Current).AnimationDuration));
            myDoubleAnimationWidth.AutoReverse = false;
            listScroll.BeginAnimation(ScrollViewer.WidthProperty, myDoubleAnimationWidth);
     }

        private void listScroll_MouseLeave(object sender, MouseEventArgs e)
        {
            DoubleAnimation myDoubleAnimationWidth = new DoubleAnimation();
            myDoubleAnimationWidth.To = 10;
            myDoubleAnimationWidth.Duration = new Duration(TimeSpan.FromSeconds(((App)Application.Current).AnimationDuration));
            myDoubleAnimationWidth.AutoReverse = false;
            listScroll.BeginAnimation(ScrollViewer.WidthProperty, myDoubleAnimationWidth);
    }

add by 颜昌钢  20080603....

对于动画,一般是放到一个叫什么storyboard控件里面,然后,还包括很多的动画,如上的是对应doubleanimation的,及对应属性类型为double 类型的所产生的动画,当然,该属性一定是dependences的..同时,还有一些其他的动画,包括什么coloranimation等等...先记录在这个地方,以后慢慢的开篇写...
 

转载于:https://www.cnblogs.com/yanchanggang/archive/2008/05/21/1204347.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值