silverlight RadGridView总结二(转载)

系列二
    实现RadGridView行中添加不同控件,并在控件中绑定不同的数据源
    先上一段前台代码  
View Code
 <telerik:RadGridView Grid.Row="2" x:Name="activitiesGrid1"
                              AutoGenerateColumns="False" GridLinesVisibility="None" Visibility="Collapsed"
                              UseLayoutRounding="False"  IsReadOnly="True"
                              RowHeight="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionMode="Single"
                              HorizontalGridLinesBrush="Transparent" VerticalGridLinesBrush="Transparent"
                              Background="{x:Null}" BorderBrush="{x:Null}"
                              IsFilteringAllowed="False" RowIndicatorVisibility="Collapsed"
                              CanUserFreezeColumns="False" ShowGroupPanel="False"
                              HeaderRowStyle="{StaticResource SalesInfoGridViewHeaderRowStyle}"
                              GroupRowStyle="{StaticResource SalesInfoGridViewGroupRowStyle}" 
                              RowStyle="{StaticResource SalesInfoGridViewRowStyle}"
                              ScrollViewer.HorizontalScrollBarVisibility="Auto"
                              ScrollViewer.VerticalScrollBarVisibility="Auto"  Height="200">
                             <telerik:RadGridView.Columns>
                                 <telerik:GridViewColumn Header="归属营业厅ID" Width="*" IsVisible="False">
                                     <telerik:GridViewColumn.CellTemplate>
                                         <DataTemplate>
                                             <TextBlock  Text="{Binding ROOM_ID}" />
                                         </DataTemplate>
                                     </telerik:GridViewColumn.CellTemplate>
                                 </telerik:GridViewColumn>
                                 <telerik:GridViewColumn Header="归属县ID" Width="*" IsVisible="False">
                                     <telerik:GridViewColumn.CellTemplate>
                                         <DataTemplate>
                                             <TextBlock  Text="{Binding TOWN_ID}" />
                                         </DataTemplate>
                                     </telerik:GridViewColumn.CellTemplate>
                                 </telerik:GridViewColumn>
                                 <telerik:GridViewColumn Header="归属营业厅名称" Width="*">
                                     <telerik:GridViewColumn.CellTemplate>
                                         <DataTemplate>
                                             <TextBlock  Text="{Binding ROOM_NAME}" />
                                         </DataTemplate>
                                     </telerik:GridViewColumn.CellTemplate>
                                 </telerik:GridViewColumn>
                                 <telerik:GridViewColumn Header="归属县名称" Width="*">
                                     <telerik:GridViewColumn.CellTemplate>
                                         <DataTemplate>
                                             <TextBlock Text="{Binding COUNTY_NAME}" FontSize="14"  Width="100"/>
                                         </DataTemplate>
                                     </telerik:GridViewColumn.CellTemplate>
                                 </telerik:GridViewColumn>
                                 <telerik:GridViewColumn Header="归属营业厅类型" Width="*">
                                     <telerik:GridViewColumn.CellTemplate>
                                         <DataTemplate>
                                             <telerik:RadComboBox EmptyText="{Binding ROOM_TYPE}"  Width="100" FontSize="14" Text="{Binding  ROOM_TYPE}"
                                                                  HorizontalAlignment="Left" Style="{StaticResource RadComboBoxStyle1}" >
                                                 <telerik:RadComboBoxItem Content="形象厅"  Style="{StaticResource RadComboBoxItemStyle1}" />
                                                 <telerik:RadComboBoxItem Content="市县营业厅"  Style="{StaticResource RadComboBoxItemStyle1}"/>
                                                 <telerik:RadComboBoxItem Content="乡镇营业厅"  Style="{StaticResource RadComboBoxItemStyle1}"/>
                                             </telerik:RadComboBox>
                                         </DataTemplate>
                                     </telerik:GridViewColumn.CellTemplate>
                                 </telerik:GridViewColumn>
                                 <telerik:GridViewColumn Header="创建时间" Width="*">
                                     <telerik:GridViewColumn.CellTemplate>
                                         <DataTemplate>
                                             <TextBlock  Text="{Binding CREATETIME}" />
                                         </DataTemplate>
                                     </telerik:GridViewColumn.CellTemplate>
                                 </telerik:GridViewColumn>
                                 <telerik:GridViewColumn Header="状态" Width="*">
                                     <telerik:GridViewColumn.CellTemplate>
                                         <DataTemplate>
                                             <telerik:RadComboBox EmptyText="{Binding ROOM_STAT}" Text="{Binding ROOM_STAT}" Width="100" FontSize="14"  HorizontalAlignment="Left" Style="{StaticResource RadComboBoxStyle1}" >
                                                 <telerik:RadComboBoxItem Content="正常"  Style="{StaticResource RadComboBoxItemStyle1}" />
                                                 <telerik:RadComboBoxItem Content="异常"  Style="{StaticResource RadComboBoxItemStyle1}"/>
                                             </telerik:RadComboBox>
                                         </DataTemplate>
                                     </telerik:GridViewColumn.CellTemplate>
                                 </telerik:GridViewColumn>
                                 <telerik:GridViewDataColumn DataMemberBinding="{Binding ROOM_DMXS}" Header="营业厅店面调节系数" Width="*" />
                                 <telerik:GridViewColumn Header="是否3人以上营业厅" Width="*">
                                     <telerik:GridViewColumn.CellTemplate>
                                         <DataTemplate>
                                             <telerik:RadComboBox EmptyText="{Binding IS_GREATERTHREE}" Text="{Binding IS_GREATERTHREE}" Width="100"
                                                                  FontSize="14"  HorizontalAlignment="Left" Style="{StaticResource RadComboBoxStyle1}" >
                                                 <telerik:RadComboBoxItem Content="是"  Style="{StaticResource RadComboBoxItemStyle1}" />
                                                 <telerik:RadComboBoxItem Content="否"  Style="{StaticResource RadComboBoxItemStyle1}"/>
                                             </telerik:RadComboBox>
                                         </DataTemplate>
                                     </telerik:GridViewColumn.CellTemplate>
                                 </telerik:GridViewColumn>
                                 <telerik:GridViewColumn Header="" Width="*"     HeaderCellStyle="{StaticResource SalesInfoGridViewHeaderCellStyleFirst}"
                                                         CellStyle="{StaticResource SalesInfoGridViewCellStyle}">
                                     <telerik:GridViewColumn.CellTemplate>
                                         <DataTemplate>
                                             <telerik:RadButton   Style="{StaticResource RadButtonStyle}" Content="删除"
                                                              x:Name="ExceptXishu" Click="ExceptXishu_Click"
                                                              FontSize="12" Foreground="White" 
                                                              Tag="{Binding ID}" 
                                                              Height="20" Width="50"
                                                              />
                                         </DataTemplate>
                                     </telerik:GridViewColumn.CellTemplate>
                                 </telerik:GridViewColumn>
                             </telerik:RadGridView.Columns>
                         </telerik:RadGridView>

在GridViewColumn中添加GridViewColumn.CellTemplate,并在其子节点添加DataTemplate,在其中添加你想要的控件。可以在不同的控件中设置数据源绑定。

我在给RadGridView赋值的时候是通过赋值LIST集合赋值。

例如,this.activitiesGrid.ItemsSource = e.Result.ToList();---此处指的是从服务引用中返回的数据源(大家都懂得)

若你想你的控件在展示的时候默认显示第一行,请添加如下代码:

 if (this.activitiesGrid.Items.Count>0)
 {
 this.activitiesGrid.SelectedItem = activitiesGrid.Items[0];
 }   

在后台控制控件中某一列的显示和隐藏
声明GridViewColumn 对象,取得对象之后控制显示和隐藏

GridViewColumn column = this.activitiesGrid.Columns[第几行];

column.IsVisible = true;//显示这一列

在后台取得某一行的数据源

(在这里我用了一个技巧,在给RadGridView绑定数据源的同时,我给每一个行的数据源绑定了一个ID,是为了取得每一个行的数据源)

在RadGridView控件中,每一行我定义一个控件,例如是RadButton控件,在他的Tag属性中我绑定了这一行数据源的ID,点击事件后我将这行的ID取出,然后可以得到其数据源,前后台代码如下:

前台代码:

View Code
 <telerik:GridViewColumn Header="删除" Width="*"     HeaderCellStyle="{StaticResource SalesInfoGridViewHeaderCellStyleFirst}"
                                                         CellStyle="{StaticResource SalesInfoGridViewCellStyle}">
                                 <telerik:GridViewColumn.CellTemplate>
                                     <DataTemplate>
                                         <telerik:RadButton   Style="{StaticResource RadButtonStyle}" Content="删除"
                                                              FontSize="12" Foreground="White" 
                                                              Tag="{Binding ID}" 
                                                              Height="20" Width="50"
                                                              Click="RadButton_Click_2" />
                                     </DataTemplate>
                                 </telerik:GridViewColumn.CellTemplate>
                             </telerik:GridViewColumn>


后台代码:

View Code 
private void btDeny_Click(object sender, RoutedEventArgs e)
{
RadButton btn = sender as RadButton;
int num = Convert.ToInt32(btn.TabIndex.ToString());
GridViewRow row=new GridViewRow();
var item = this.BackUserDetailes.Items[num];//绑定数据源的同时,给从库中返回的数据源制定了ID
row=this.BackUserDetailes.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;
//得到Row对象后可以将其的数据源转换成你想要的类型
想要的类型=row.DataContext as 想要的类型;
}

也可以用另外一种简单的方式获得这一行的数据源
想要的类型=btn.DataContext as 想要的类型。

 在后台获取行中有不同控件值的方法

取得了GridViewRow对象之后,可以取得这一行的每个单元格,即可把单元格中的控件取出。代码如下:

例如,你在这一单元格中放置的是RadComboBox控件,可以通过:RadComboBox box=row.Cells[第几个单元格].Content as RadComboBox;将RadComboBox对象取出,并进行你想要的操作。

在RadGridView中对某一列的特殊的控件赋值,通过这一列的控件中声明其Loaded事件,为这一行的特殊的控件赋值。

例如下例:

View Code
  <telerik:GridViewColumn Header="给特殊列绑定单个数据" Width="*"     HeaderCellStyle="{StaticResource SalesInfoGridViewHeaderCellStyleFirst}"
                                                         CellStyle="{StaticResource SalesInfoGridViewCellStyle}" >
                                 <telerik:GridViewColumn.CellTemplate>
                                     <DataTemplate>
                                         <telerik:RadComboBox EmptyText="{Binding 绑定的数据源中字段名称}"  Text="{Binding 绑定的数据源中字段名称}"
                                                              FontSize="14" Width="200" HorizontalAlignment="Left"
                                                              Style="{StaticResource RadComboBoxStyle1}"   
                                                              Loaded="RadComboBox_Loaded">
                                             <telerik:RadComboBoxItem Content="{Binding 绑定的数据源中字段名称}" Tag="{Binding CHNL_ID}" Style="{StaticResource RadComboBoxItemStyle1}" />
                                         </telerik:RadComboBox>
                                     </DataTemplate>
                                 </telerik:GridViewColumn.CellTemplate>
                             </telerik:GridViewColumn>


其中,EmptyText="{Binding 绑定的数据源的名称}"指的是其RadComboBox在没有 默认显示第一个值时的赋予该控件的值。
后台代码如下:

View Code
  private void RadComboBox_Loaded(object sender, RoutedEventArgs e)
         {
             RadComboBox box = sender as RadComboBox;
             box.Items.Clear();
             (sender as RadComboBox).DisplayMemberPath = "ROOM_NAME";
             (sender as RadComboBox).Tag = this.chnlList[0].ROOM_ID;
             (sender as RadComboBox).ItemsSource = 数据源名称;          
             box.ItemsSource = chnlList;
         }


posted @ 2013-08-29 16:57  sandea  阅读(305)  评论(0编辑  收藏  举报