演练:使用属性自定义 DataGrid 控件
演练:使用属性自定义 DataGrid 控件
Silverlight DataGrid 控件支持常用表格式设置选项,例如交替显示不同的行背景以及能够显示或隐藏表头、网格线和滚动条。 可以通过设置 DataGrid 属性来进行这些自定义。 属性可以在设计时在 XAML 中设置,也可以在运行时在过程代码中设置。
本演练演示以下任务:
若要查看 DataGrid 控件的运行示例,请单击下面的链接。
您需要以下组件来完成本演练:
Silverlight 5 Beta.
用于 Visual Studio 2010 的 Silverlight Tools.
Visual Studio 2010.
可以从 Silverlight 下载站点 下载所有 Silverlight 软件。
第一步是创建一个 Silverlight 项目。
创建 Silverlight 项目
使用 Visual Basic 或 Visual C# 新建一个名为 DGBasicCustomization 的 Silverlight 应用程序项目。 保持选中默认选项“在新网站中承载 Silverlight 应用程序”。 有关更多信息,请参见 如何创建新 Silverlight 项目。
接下来创建要在 DataGrid 中显示的 Task 对象的集合。
创建 Task 对象的集合
向 DGBasicCustomization 项目中添加一个名为 Task 的类。
向 Task 类中添加下面的代码。
此代码包含 Task 类,该类表示要在 DataGrid 控件中显示的数据对象。
打开 MainPage.xaml.vb 或 MainPage.xaml.cs。
在 MainPage 类构造函数的 InitializeComponent 方法的后面添加以下代码。
此代码创建一个名为 taskList 的泛型 List<T>,并使用循环以 Task 对象填充集合。 然后将 taskList 设置为 DataGrid的 ItemsSource。
// Create a list to store task data.
List<Task> taskList = new List<Task>();
int itemsCount = 10;
// Generate some task data and add it to the task list.
for (int i = 1; i <= itemsCount; i++)
{
taskList.Add(new Task()
{
Name = "Task " + i.ToString(),
DueDate = DateTime.Now.AddDays(i),
Complete = (i % 3 == 0),
Notes = "Task " + i.ToString() + " is due on "
+ DateTime.Now.AddDays(i) + ". Lorum ipsum..."
});
}
this.dataGrid1.ItemsSource = taskList;
接下来,通过向页面中添加 DataGrid 控件来创建任务列表的用户界面。
创建任务列表的用户界面
在 DGBasicCustomization 项目中,添加一个对 System.Windows.Controls.Data 程序集的引用。 有关更多信息,请参见 如何:向页中添加 DataGrid 控件。
打开 MainPage.xaml。
在 <UserControl> 开始标记中添加下面的 XAML。
此 XAML 将 sdk 前缀映射到 Silverlight SDK 命名空间,如 Silverlight 库的前缀和映射中所述。
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
在 <UserControl> 开始标记中,将 Width 属性的值更改为 600,将 Height 属性的值更改为 700。
在 <UserControl> 开始标记之后添加下面的 XAML,从而替换现有的 <Grid> 标记。
此 XAML 添加一个 StackPanel 以及一个名为 dataGrid1 的 DataGrid,前者用于布局目的,后者用于显示任务列表。
<StackPanel x:Name="LayoutRoot" Background="White" Margin="5"> ... <sdk:DataGrid x:Name="dataGrid1" Height="350" Width="540"
HorizontalAlignment="Left" >
</sdk:DataGrid>
</StackPanel>生成并运行此应用程序。
当应用程序运行时,您将看到一个 DataGrid,它以其默认外观和行为显示 taskList 集合中的任务。
接下来创建 DataGrid 选项的用户界面。
创建 DataGrid 选项的用户界面
打开 MainPage.xaml。
在 <StackPanel> 开始标记之后添加下面的 XAML。
此 XAML 用于添加选择控件,您将使用这些控件在运行时修改 DataGrid 的属性。
<StackPanel Margin="0,0,0,5">
<StackPanel Orientation="Horizontal">
<Border BorderBrush="Black" BorderThickness="1" Padding="3" Width="180">
<StackPanel>
<TextBlock Text="DataGrid Options" FontSize="12" />
<CheckBox Content="Grid is read-only"
Checked="cbReadOnly_Changed" Unchecked="cbReadOnly_Changed" />
<CheckBox Content="Freeze first column"
Checked="cbFreezeColumn_Changed" Unchecked="cbFreezeColumn_Changed"/>
<CheckBox Content="Hide first column"
Checked="cbHideColumn_Changed" Unchecked="cbHideColumn_Changed"/>
<CheckBox Content="Single Row Selection"
Checked="cbSelectionMode_Changed" Unchecked="cbSelectionMode_Changed" />
<TextBlock Text="Column Options" FontSize="12" />
<CheckBox Content="Allow Column Reordering" IsChecked="true"
Checked="cbColReorder_Changed" Unchecked="cbColReorder_Changed"/>
<CheckBox Content="Allow Column Resizing" IsChecked="true"
Checked="cbColResize_Changed" Unchecked="cbColResize_Changed"/>
<CheckBox Content="Allow Column Sorting" IsChecked="true"
Checked="cbColSort_Changed" Unchecked="cbColSort_Changed"/>
<TextBlock Text="Scroll Bar Options" FontSize="12" />
<CheckBox Content="Vertical Scroll Bars" IsThreeState="True" IsChecked=""
Checked="cbVerticalScroll_Changed" Unchecked="cbVerticalScroll_Changed"
Indeterminate="cbVerticalScroll_Changed" />
<CheckBox Content="Horizontal Scroll Bars" IsThreeState="True" IsChecked=""
Checked="cbHorizontalScroll_Changed" Unchecked="cbHorizontalScroll_Changed"
Indeterminate="cbHorizontalScroll_Changed"/>
</StackPanel>
</Border>
<Border BorderBrush="Black" BorderThickness="1" Padding="3" Width="180">
<StackPanel>
<TextBlock Text="Alternating Row Background" FontSize="12" />
<ComboBox SelectionChanged="cbAltRowBrush_SelectionChanged">
<ComboBoxItem Content="Default" IsSelected="True" />
<ComboBoxItem Content="Custom" />
<ComboBoxItem Content="Null" />
</ComboBox>
<TextBlock Text="Row Background" FontSize="12" />
<ComboBox SelectionChanged="cbRowBrush_SelectionChanged">
<ComboBoxItem Content="Default" IsSelected="True" />
<ComboBoxItem Content="Custom" />
<ComboBoxItem Content="Null" />
</ComboBox>
<TextBlock Text="Header Visibility" FontSize="12" />
<ComboBox SelectionChanged="cbHeaders_SelectionChanged">
<ComboBoxItem Content="All"/>
<ComboBoxItem Content="Column (Default)" IsSelected="True"/>
<ComboBoxItem Content="Row"/>
<ComboBoxItem Content="None"/>
</ComboBox>
<TextBlock Text="Grid Lines Visibility" FontSize="12" />
<ComboBox SelectionChanged="cbGridLines_SelectionChanged">
<ComboBoxItem Content="All"/>
<ComboBoxItem Content="Vertical (Default)" IsSelected="True"/>
<ComboBoxItem Content="Horizontal"/>
<ComboBoxItem Content="None"/>
</ComboBox>
<TextBlock Text="Custom Grid Lines" FontSize="12" />
<CheckBox Content="Vertical"
Checked="cbCustomGridLineVert_Changed"
Unchecked="cbCustomGridLineVert_Changed"/>
<CheckBox Content="Horizontal"
Checked="cbCustomGridLineHorz_Changed"
Unchecked="cbCustomGridLineHorz_Changed"/>
</StackPanel>
</Border>
</StackPanel>
</StackPanel>
接下来,您将添加代码以处理对用户界面控件所做的更改以及设置 DataGrid 属性。
在代码中设置 DataGrid 属性
打开 MainPage.xaml.vb 或 MainPage.xaml.cs。
在 MainPage 类构造函数之后,添加下面的代码。
此代码将处理您在上一节中添加的 DataGrid 选项用户界面控件的更改事件。 当某个选项更改时,将设置相应的DataGrid 属性
// READ ONLY
private void cbReadOnly_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
this.dataGrid1.IsReadOnly = (bool)cb.IsChecked;
}
// FREEZE COLUMN
private void cbFreezeColumn_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
{
if (cb.IsChecked == true)
this.dataGrid1.FrozenColumnCount = 1;
else if (cb.IsChecked == false)
this.dataGrid1.FrozenColumnCount = 0;
}
}
// HIDE COLUMN
private void cbHideColumn_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
{
if (cb.IsChecked == true)
this.dataGrid1.Columns[0].Visibility = Visibility.Collapsed;
else if (cb.IsChecked == false)
this.dataGrid1.Columns[0].Visibility = Visibility.Visible;
}
}
// SELECTION MODE
private void cbSelectionMode_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
{
if (cb.IsChecked == true)
this.dataGrid1.SelectionMode = DataGridSelectionMode.Single;
else if (cb.IsChecked == false)
this.dataGrid1.SelectionMode = DataGridSelectionMode.Extended;
}
}
// COLUMN OPTIONS
private void cbColReorder_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
this.dataGrid1.CanUserReorderColumns = (bool)cb.IsChecked;
}
private void cbColResize_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
this.dataGrid1.CanUserResizeColumns = (bool)cb.IsChecked;
}
private void cbColSort_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
this.dataGrid1.CanUserSortColumns = (bool)cb.IsChecked;
}
// SCROLL BARS VISIBILITY
private void cbVerticalScroll_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
{
if (cb.IsChecked == true)
this.dataGrid1.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
else if (cb.IsChecked == false)
this.dataGrid1.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
else
this.dataGrid1.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
}
}
private void cbHorizontalScroll_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
{
if (cb.IsChecked == true)
this.dataGrid1.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
else if (cb.IsChecked == false)
this.dataGrid1.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
else
this.dataGrid1.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
} }
// ROW BRUSH
private void cbAltRowBrush_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem cbi = ((ComboBox)sender).SelectedItem as ComboBoxItem; if (this.dataGrid1 != null)
{
if (cbi.Content.ToString() == "Custom")
this.dataGrid1.AlternatingRowBackground = new SolidColorBrush(Color.FromArgb(255, 130, 175, 200));
else if (cbi.Content.ToString() == "Default")
this.dataGrid1.AlternatingRowBackground = new SolidColorBrush(Color.FromArgb(37, 233, 238, 244));
else if (cbi.Content.ToString() == "Null")
this.dataGrid1.AlternatingRowBackground = null;
}
}
private void cbRowBrush_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem cbi = ((ComboBox)sender).SelectedItem as ComboBoxItem;
if (this.dataGrid1 != null)
{
if (cbi.Content.ToString() == "Custom")
this.dataGrid1.RowBackground = new SolidColorBrush(Color.FromArgb(255, 135, 185, 195));
else if (cbi.Content.ToString() == "Default")
this.dataGrid1.RowBackground = new SolidColorBrush(Color.FromArgb(00, 255, 255, 255));
else if (cbi.Content.ToString() == "Null")
this.dataGrid1.RowBackground = null;
}
}
// HEADERS VISIBILITY
private void cbHeaders_SelectionChanged(object sender, RoutedEventArgs e)
{
ComboBoxItem cbi = ((ComboBox)sender).SelectedItem as ComboBoxItem;
if (this.dataGrid1 != null)
{
if (cbi.Content.ToString() == "All")
this.dataGrid1.HeadersVisibility = DataGridHeadersVisibility.All;
else if (cbi.Content.ToString() == "Column (Default)")
this.dataGrid1.HeadersVisibility = DataGridHeadersVisibility.Column;
else if (cbi.Content.ToString() == "Row")
this.dataGrid1.HeadersVisibility = DataGridHeadersVisibility.Row;
else
this.dataGrid1.HeadersVisibility = DataGridHeadersVisibility.None;
} }
// GRIDLINES VISIBILITY
private void cbGridLines_SelectionChanged(object sender, RoutedEventArgs e)
{
ComboBoxItem cbi = ((ComboBox)sender).SelectedItem as ComboBoxItem;
if (this.dataGrid1 != null)
{
if (cbi.Content.ToString() == "All")
this.dataGrid1.GridLinesVisibility = DataGridGridLinesVisibility.All;
else if (cbi.Content.ToString() == "Vertical (Default)")
this.dataGrid1.GridLinesVisibility = DataGridGridLinesVisibility.Vertical;
else if (cbi.Content.ToString() == "Horizontal")
this.dataGrid1.GridLinesVisibility = DataGridGridLinesVisibility.Horizontal;
else
this.dataGrid1.GridLinesVisibility = DataGridGridLinesVisibility.None;
} }
// CUSTOM GRIDLINES
private void cbCustomGridLineVert_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
{
if (cb.IsChecked == true)
this.dataGrid1.VerticalGridLinesBrush = new SolidColorBrush(Colors.Blue);
else
this.dataGrid1.VerticalGridLinesBrush = new SolidColorBrush(Color.FromArgb(255, 223, 227, 230));
}
}
private void cbCustomGridLineHorz_Changed(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (this.dataGrid1 != null)
{
if (cb.IsChecked == true)
this.dataGrid1.HorizontalGridLinesBrush = new SolidColorBrush(Colors.Blue);
else
this.dataGrid1.HorizontalGridLinesBrush = new SolidColorBrush(Color.FromArgb(255, 223, 227, 230));
}
}生成并运行此应用程序。
当应用程序运行时,您将看到一个 DataGrid,其中显示 taskList 集合中的任务。 此外,您还将看到用于动态修改DataGrid 控件的外观和行为的选项。
当应用程序运行时,更改各个选项并查看对 DataGrid 控件的外观和行为产生的影响。
如果您无需在运行时动态设置 DataGrid 属性,则通常在 XAML 中设置这些属性。 若要查看 XAML 语法来设置属性,请参见DataGrid 属性文档的"XAML 属性用法"部分。
也可以通过向控件应用样式和模板来自定义 DataGrid。 利用样式和模板,可以对控件实现比设置属性更多的自定义。 若要了解有关如何向控件应用样式和模板的更多信息,请参见控件自定义和 DataGrid 样式和模板。
演练:使用属性自定义 DataGrid 控件的更多相关文章
- WPF 自定义DataGrid控件样式
内容转自https://www.cnblogs.com/xiaogangqq123/archive/2012/05/07/2487166.html 一.DataGrid基本样式(一) 小刚已经把Dat ...
- jQuery之自定义datagrid控件
sldatagrid 效果: sldatagrid.js (function($) { function loadColumns(sldatagrid, columns) { $(sldatagrid ...
- WPF 4 DataGrid 控件(自定义样式篇)
原文:WPF 4 DataGrid 控件(自定义样式篇) 在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...
- EasyUI 中 DataGrid 控件 列 如何绑定对象中的属性
EasyUI 中 DataGrid 控件 是我们经常用到的控件之一, 但是 DataGrid 控件 在绑定显示列时却不支持对象属性绑定. 模型如下: public class Manager impl ...
- wpf研究之道——datagrid控件分页
这是我们的datagrid分页效果图,有上一页,下一页,可以跳到任何一页.当页码比较多的时候,只显示几页,其余用点点,界面实现如下: <!--分页--> <StackPanel Or ...
- WPF 4 DataGrid 控件(进阶篇一)
原文:WPF 4 DataGrid 控件(进阶篇一) 上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...
- WPF 4 DataGrid 控件(进阶篇二)
原文:WPF 4 DataGrid 控件(进阶篇二) 上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...
- WPF 4 DataGrid 控件(基本功能篇)
原文:WPF 4 DataGrid 控件(基本功能篇) 提到DataGrid 不管是网页还是应用程序开发都会频繁使用.通过它我们可以灵活的在行与列间显示各种数据.本篇将详细介绍WPF 4 中 ...
- 安卓自定义组合控件--toolbar
最近在学习安卓APP的开发,用到了toolbar这个控件, 最开始使用时include layout这种方法,不过感觉封装性不好,就又改成了自定义组合控件的方式. 使用的工具为android stud ...
随机推荐
- CodeForces 22、23部分题解
CodeForces 22A 找严格第二小的...注意只有一种情况,可以sort排序然后unique输出. int a[N]; int main() { int n; while(~scanf(&qu ...
- Codeforces 892 B.Wrath
B. Wrath time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- HDU 3763 CDs
http://acm.hdu.edu.cn/showproblem.php?pid=3763 题意: 两组数据 看重复的有多少 如果每输入一个就去查找的话O(n^2) 会超时 所以可以用二法 第一组数 ...
- C#.net获取存储过程的Return返回值和Output输出参数值
原文发布时间为:2008-10-25 -- 来源于本人的百度文章 [由搬家工具导入] 1.获取Return返回值 程序代码//存储过程//Create PROCEDURE MYSQL// @a ...
- ES6__Iterator和for...of循环
/** * Iterator和for...of循环 */ // --------------------------------------------------------------- /** ...
- JS中的call()和apply()方法区别
如 果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的事偏偏就发生了,甚至觉得不可 理喻.如果在学JavaScript这自由而变幻无穷 ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]
传送门 C. Om Nom and Candies time limit per test 1 second memory limit per test 256 megabytes input sta ...
- php 以单下划线或双下划线开头的命名
有2个下划线的是魔术方法,如:__construct.__destruct等等.有1个下划线的一般是私有方法,如 _initialize. 小测试: public function _test(){ ...
- The Doors--poj1556(最短路+判断点与线段的关系)
http://poj.org/problem?id=1556 题目大意:从(0,5)走到(10,5)走的最短距离是多少 中间有最多18个隔着的墙 每个墙都有两个门 你只能从门通过 我的思路是 只 ...
- sql 2005 安装
http://blog.csdn.net/wochuailimin/article/details/6120462 http://www.cnblogs.com/huangcong/archive/2 ...