今天下午,在做奥运会保障系统的时候,用到了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等等...先记录在这个地方,以后慢慢的开篇写...