您的位置:首页 > 其它

Silverlight控件用法合集DataGrid,ComboBox,DataPick,CheckBox,RadioButtonSilverlight控件用法合集DataGrid,ComboBox,DataPick,CheckBox,RadioButton

2012-05-13 12:49 423 查看
平台 :Vs 2010,Blend 4

OK,代码下载:http://files.cnblogs.com/Royal_WH/BlogWebTest.rar

技术性不是很强,但是很实用,题目想好了,明天来写,以后每天补充一点吧!

好了,先来介绍DataGrid吧,毕竟大家都要用么!

先看下他的属性吧(对于画笔、外观、布局方面的属性我就不介绍了,毕竟是一些公共属性想必大家已经比较了解了):

重要属性:

1.ItemSource 设置数据源。

用法: dataGrid.ItemSource = 数据源集合;

2.AutoGenerateColumns 是否在绑定数据源后自动生成列。

用法:直接在Xmal中附加上这一属性 true or false。

3.Columns 不用多说了,就是列集合。

用法:在Xmal中写入以下内容

<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn/>
</sdk:DataGrid.Columns>

当然这里的sdk:DataGridTextColumn也可以是模板列和CheckBoxColumn,关于模板列我们后面再讲解。

4.HeadersVisibility 标题是否可见。

用法:xmal中写,All是行列都可见,None是都不可见,Column和Row分别是列和行可见。

5.GridLinesVisibility 是否显示网格线

用法:Xmal中写,All是行列都可见,None是都不可见,Horizontal是行可见,Vertical是列可见

6.SelectedIndex 当前选中的索引编号

用法:C# 中 dataGrid.SelectedIndex 获取或设置

7.SelectionMode 当前选择模式单选(single)或多选(Extended)

用法:Xmal中写标记

8.CanUserReorderColumns 是否可拖动列和改变列顺序

用法:Xmal中写标记

9.CanUserResizeColumns 是否可以改变列的大小

用法:Xmal中写标记

好了还有一些什么 IsReadyOnly,IsEnabled,IsHitTestVisible,CanUserSortColumns大家一看就明白的我就不过多讲了!

下面写个绑定数据的小例子,由于是测试我就不用WCF或WebService来传数据了。先上图:

代码

<UserControl x:Class="SilverlightApplication.MainPage"
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="419" d:DesignWidth="642" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

<Grid x:Name="LayoutRoot" Background="White" Height="357" Width="594">
<sdk:DataGrid x:Name="dataGrid" Height="169" HorizontalAlignment="Left" Margin="88,48,0,0" VerticalAlignment="Top" Width="400" AutoGenerateColumns="True"/>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="88,272,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
</Grid>
</UserControl>

代码

publicpartialclass MainPage : UserControl
{
List<ModelTest> temp =new List<ModelTest>();
public MainPage()
{
InitializeComponent();
}
void LoadData()
{
ModelTest newModel =new ModelTest();
newModel.ID =1;
newModel.Name ="张三";
newModel.RegData = DateTime.Now;
newModel.Sex =0;
temp.Add(newModel);

newModel =new ModelTest();
newModel.ID =2;
newModel.Name ="李四";
newModel.RegData = DateTime.Now;
newModel.Sex =0;
temp.Add(newModel);

newModel =new ModelTest();
newModel.ID =3;
newModel.Name ="小王";
newModel.RegData = DateTime.Now;
newModel.Sex =1;
temp.Add(newModel);
}
privatevoid button1_Click(object sender, RoutedEventArgs e)
{
LoadData();
dataGrid.ItemsSource = temp;
}
}
publicclass ModelTest
{
publicint ID
{ set; get; }
publicstring Name
{ set; get; }

public DateTime RegData
{ set; get; }

publicbyte Sex
{ set; get; }
publicstring Image
{ set; get; }
}

privatevoid button2_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedItem !=null)
{
ModelTest model = dataGrid.SelectedItem as ModelTest;

MessageBox.Show(model.Name);
}
}

privatevoid dataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ModelTest model = e.AddedItems[0] as ModelTest;
ModelTest model2 = dataGrid.SelectedItem as ModelTest;
MessageBox.Show(model.Name);
}

代码

<sdk:DataGrid x:Name="dataGrid" Height="169" HorizontalAlignment="Left" Margin="88,48,0,0" VerticalAlignment="Top" Width="400" SelectionChanged="dataGrid_SelectionChanged">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="ID号" Binding="{Binding ID}"/>
<sdk:DataGridTextColumn Header="名称" Binding="{Binding Name}"/>
<sdk:DataGridTextColumn Header="性别" Binding="{Binding Sex}"/>
<sdk:DataGridTextColumn Header="注册时间" Binding="{Binding RegData}"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>

代码

int length = e.Result.Count;
yearComboBox.Items.Clear();
for (int i =0; i < length; i++)
{
yearComboBox.Items.Add(e.Result[i].ToString());
}

代码

int length = e.Result.Count;
yearComboBox.Items.Clear();
for (int i =0; i < length; i++)
{
yearComboBox.Items.Add(new new ComboBoxItem(){Content = "显示值",Tag="属性值"});
}




然后获取的时候

ComboBoxItem item = yearComboBox.SelectedItem as ComboBoxItem;
item.Tag //获取属性值
item.Content // 获取显示值

然后说一下事件:

yearComboBox_SelectionChanged,其实发觉其它事件用的很少真的没什么事说的,改变事件SelectionChanged,选择值发生改变时引发

DataPick

DisplayDateEnd,显示最大日期

DisplayDateStart,显示最小日期

<sdk:DatePicker x:Name="datePicker" Height="26" SelectedDateFormat="Long" Margin="308,8,150,0" VerticalAlignment="Top" IsDropDownOpen="False"/>

datePicker.SelectedDate.ToString()

就可以获取当前选择时间。

主要事件:

SelectedDateChanged 选择日期改变后引发这个事件。

CheckBox

<CheckBox x:Name="checkBox" Content="CheckBox" Checked="CheckBox_Checked" HorizontalAlignment="Right" Margin="0,11,52,0" VerticalAlignment="Top"/>

要判断CheckBox状态 checkBox.IsChecked

重要事件:Checked,UnChecked

三选状态:IsThreeState="True"

RadioButton

其实他和CheckBox差不多了

<RadioButton Content="RadioButton" HorizontalAlignment="Right" Margin="0,48,39,0" VerticalAlignment="Top" IsThreeState="True" Checked="RadioButton_Checked"/>

要判断CheckBox状态 checkBox.IsChecked

重要事件:Checked,UnChecked

三选状态:IsThreeState="True"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐