Silverlight控件DataGrid用法总结
常规的用法先总结一下。前台设置列名,列宽,列高,绑定数据。后台构造数据源,设置是否排序等。
其他的小技巧遇到了总结到此文。如图:
uc_DataGrid.xaml
大气象
<UserControl x:Class="HCLoad.uc_DataGrid"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="True" Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="dgList" VerticalAlignment="Top" Width="300">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Id" Binding="{Binding 编号}" Width="100"></sdk:DataGridTextColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
</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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="True" Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="dgList" VerticalAlignment="Top" Width="300">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Id" Binding="{Binding 编号}" Width="100"></sdk:DataGridTextColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
</UserControl>
uc_DataGrid.xaml.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;
namespace HCLoad
{
public partial class uc_DataGrid : UserControl
{
public uc_DataGrid()
{
InitializeComponent();
Bind();
}
List<Model> list = new List<Model>();
public class Model
{
public int ID { get; set; }
public string 名称 { get; set; }
public Model() { }
public Model(int _ID, string _名称)
{
ID = _ID;
名称 = _名称;
}
}
private void Bind()
{
list.Clear();
list.Add(new Model(1, "名1"));
list.Add(new Model(2, "名2"));
list.Add(new Model(3, "名3"));
dgList.ItemsSource = list;
dgList.CanUserSortColumns = false;//设置不排序
dgList.Height = 150;
dgList.IsReadOnly = true;//设置只读,选择的时候,不能进入编辑状态
}
}
}
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 HCLoad
{
public partial class uc_DataGrid : UserControl
{
public uc_DataGrid()
{
InitializeComponent();
Bind();
}
List<Model> list = new List<Model>();
public class Model
{
public int ID { get; set; }
public string 名称 { get; set; }
public Model() { }
public Model(int _ID, string _名称)
{
ID = _ID;
名称 = _名称;
}
}
private void Bind()
{
list.Clear();
list.Add(new Model(1, "名1"));
list.Add(new Model(2, "名2"));
list.Add(new Model(3, "名3"));
dgList.ItemsSource = list;
dgList.CanUserSortColumns = false;//设置不排序
dgList.Height = 150;
dgList.IsReadOnly = true;//设置只读,选择的时候,不能进入编辑状态
}
}
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。