没文化真可怕--Silverlight 列冻结
今天接到一个需求,需要由于表格(封装的DataGrid)中的列太多,下面有滚动条,用户要求冻结前几列,便于数据查看,就像Excel那个冻结效果一新。
由于以前在“ComponnetOne”这个第方插件中见过,不知道怎么想的就以为DataGrid 不能实现,就去网上找实现的方式,当然说什么的都有,就是没有比较理想的解决方案...
一个偶然的发现: DataGrid居然有"FrozenColumnCount"属性...,这货居然有这个属性。决定试一把..
XAML:
<UserControl x:Class="SilverlightApplication1.SilverlightControl1" 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:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <StackPanel> <Button x:Name="Button1" Content="FillData" Click="Button1_Click" Height="30" Width="75" Margin="20, 10, 0, 20"/> <sdk:DataGrid x:Name="DataGrid1" FrozenColumnCount="2" AutoGenerateColumns="False"> <sdk:DataGrid.Columns> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=FirstName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=FirstName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=FirstName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=FirstName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=FirstName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=LastName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="121" Binding="{Binding Path=Age}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=IsActive}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=LastName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=Age}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=IsActive}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=LastName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=Age}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=IsActive}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=LastName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=Age}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=IsActive}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=LastName}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=Age}"></sdk:DataGridTextColumn> <sdk:DataGridTextColumn Width="120" Binding="{Binding Path=IsActive}"></sdk:DataGridTextColumn> </sdk:DataGrid.Columns> </sdk:DataGrid> </StackPanel> </Grid> </UserControl>
后台代码
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; namespace SilverlightApplication1 { public partial class SilverlightControl1 : UserControl { public SilverlightControl1() { InitializeComponent(); DataGrid dg = new DataGrid(); dg.FrozenColumnCount = 1; } public void Button1_Click(object sender, RoutedEventArgs e) { FillData(); } private void FillData() { IList<Person> list = new List<Person>(); Person p = new Person { Age = 1, FirstName = "Joe", LastName = "Elan", IsActive = true }; Person p1 = new Person { Age = 15, FirstName = "Mike", LastName = "Bull", IsActive = true }; Person p2 = new Person { Age = 51, FirstName = "Mukul", LastName = "Alwa", IsActive = true }; Person p3 = new Person { Age = 31, FirstName = "Sheo", LastName = "Narayan", IsActive = true }; list.Add(p); list.Add(p1); list.Add(p2); list.Add(p3); DataGrid1.ItemsSource = list; } public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public bool IsActive { get; set; } } } }
效果:
居然实现了……,白白浪费了半天时间,以后有问题多思考,然后再网络!