【原创】Silverlight DataGrid对中心控件DataGrid的轻易单元格进行得与安装分析。

眼前几乎龙,公司同事发生个需要需要针对系统受到的DataGrid控件的指定单元格(如图,申请人ID)进行禁用设置,尝试了成千上万差连续
整行整列的 禁用 没实现力量。 网上资料比少,没找到解决方法。

尽管silverlight的datagrid控件和
asp.net的名一样,但不少端要不大一样的。

但是换个考虑方法, 任何业务都是由对象
构成,Asp.net中的datagrid的独自元格是cell对象,所以 silverlight的单元格
应该为是 一个目标,一栽档次,可以经一些方法吃获取到。

 

几度尝后经以下的 代码方案简单明了的 找到了 目标条件的 单元格
DataGridCell.aspx)对象。

 

房地产公司 1

 

前端xaml代码:

房地产公司 2房地产公司 3

 1 <sdk:DataGrid x:Name="dataGrid" SelectionMode="Single" Grid.Row="1" Margin="-1,-1,-1,0" CopyingRowClipboardContent="dataGrid_CopyingRowClipboardContent"
 2                                       AutoGenerateColumns="False" VerticalContentAlignment="Stretch" Loaded="dataGrid2_Loaded"
 3                                       HorizontalContentAlignment="Stretch" Grid.ColumnSpan="2" CellEditEnded="dataGrid_CellEditEnded"
 4                                       ColumnHeaderStyle="{StaticResource DataGridHeaderStyle}" LoadingRow="dataGrid_LoadingRow" BeginningEdit="dataGrid_BeginningEdit"
 5                                       CurrentCellChanged="dataGrid_CurrentCellChanged">
 6                             <sdk:DataGrid.Columns>
 7                                 <sdk:DataGridTemplateColumn Width="100" Header="查询对象">
 8                                     <sdk:DataGridTemplateColumn.CellTemplate>
 9                                         <DataTemplate>
10                                             <TextBlock Height="20" Margin="2,0" VerticalAlignment="Center" HorizontalAlignment="Left" 
11                                                Text="{Binding QueryType, Converter={StaticResource QueryTypeConverter}, Mode=TwoWay}" />
12                                         </DataTemplate>
13                                     </sdk:DataGridTemplateColumn.CellTemplate>
14                                     <sdk:DataGridTemplateColumn.CellEditingTemplate>
15                                         <DataTemplate>
16                                             <ComboBox ItemsSource="{Binding KeyNameList, Source={StaticResource QueryTypeConverter}}"
17                                                       SelectedItem="{Binding QueryType, Converter={StaticResource QueryTypeConverter}, Mode=TwoWay}"/>
18                                         </DataTemplate>
19                                     </sdk:DataGridTemplateColumn.CellEditingTemplate>
20                                 </sdk:DataGridTemplateColumn>
21                                 <sdk:DataGridTextColumn  Width="0.3*" Header="查询内容" Binding="{Binding Content}"/>
22   
23                                 <sdk:DataGridTemplateColumn Width="70" Header="有无异常">
24                                     <sdk:DataGridTemplateColumn.CellTemplate>
25                                         <DataTemplate>
26                                             <TextBlock Margin="2,0" VerticalAlignment="Center" HorizontalAlignment="Left" 
27                                                Text="{Binding ResultDesc, Converter={StaticResource IsResultDescConverter}, Mode=TwoWay}" />
28                                         </DataTemplate>
29                                     </sdk:DataGridTemplateColumn.CellTemplate>
30                                     <sdk:DataGridTemplateColumn.CellEditingTemplate>
31                                         <DataTemplate>
32                                             <ComboBox ItemsSource="{Binding KeyNameList, Source={StaticResource IsResultDescConverter}}"
33                                                       SelectedItem="{Binding ResultDesc, Converter={StaticResource IsResultDescConverter}, Mode=TwoWay}"
34                                                       Height="25"/>
35                                         </DataTemplate>
36                                     </sdk:DataGridTemplateColumn.CellEditingTemplate>
37                                 </sdk:DataGridTemplateColumn>
38                                 <!--<sdk:DataGridTextColumn Width="0.7*" Header="备注" Binding="{Binding Path=Remark}"/>-->
39                                 <sdk:DataGridTemplateColumn Width="0.7*" Header="备注">
40                                     <sdk:DataGridTemplateColumn.CellTemplate>
41                                         <DataTemplate>
42                                             <TextBlock Text="{Binding Remark}" TextWrapping="Wrap" VerticalAlignment="Center"/>
43                                         </DataTemplate>
44                                     </sdk:DataGridTemplateColumn.CellTemplate>
45                                     <sdk:DataGridTemplateColumn.CellEditingTemplate>
46                                         <DataTemplate>
47                                             <TextBox Text="{Binding Remark, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap"/>
48                                         </DataTemplate>
49                                     </sdk:DataGridTemplateColumn.CellEditingTemplate>
50                                 </sdk:DataGridTemplateColumn>
51                                 <sdk:DataGridTemplateColumn Width="45"  Header="操作">
52                                     <sdk:DataGridTemplateColumn.CellTemplate>
53                                         <DataTemplate>
54                                             <StackPanel Orientation="Horizontal" Height="22">
55                                                 <Image x:Name="btnAdd" ToolTipService.ToolTip="新增" MouseLeftButtonUp="btnAdd_MouseLeftButtonUp" Source="/CTOP2P;component/Assets/Image/Action_LinkUnlink_Link.png" Style="{StaticResource imgButton}"/>
56                                                 <Image x:Name="btnCopy" Visibility="Collapsed" ToolTipService.ToolTip="复制" MouseLeftButtonUp="btnCopy_MouseLeftButtonUp" Source="/CTOP2P;component/Assets/Image/Action_Copy.png" Style="{StaticResource imgButton}"/>
57                                                 <Image x:Name="btnDel" ToolTipService.ToolTip="删除" MouseLeftButtonUp="btnDel_MouseLeftButtonUp" Cursor="Hand" Source="/CTOP2P;component/Assets/Image/Action_Delete_12x12.png" Height="12" Width="12" Margin="2,0"/>
58                                             </StackPanel>
59                                         </DataTemplate>
60                                     </sdk:DataGridTemplateColumn.CellTemplate>
61                                 </sdk:DataGridTemplateColumn>
62                             </sdk:DataGrid.Columns>
63                         </sdk:DataGrid>

View Code

用装dataGrid_BeginningEdit事件。

后端代码:

房地产公司 4房地产公司 5

private void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) {
            var item = e.Row.DataContext as AuditQueryInfo;
            if (item.QueryType.Equals("申请人ID")) {

                var dataGrid = sender as DataGrid;

                FrameworkElement fe = dataGrid.Columns[1].GetCellContent(item);
                if (fe == null)
                    return;
               FrameworkElement result = GetParent(fe, typeof(DataGridCell));

                if (fe != null) {
                    DataGridCell cell = (DataGridCell)result;

                    cell.Foreground = new SolidColorBrush(Colors.Green);
                    cell.IsEnabled = false; 
                }
            }
        }

View Code

private FrameworkElement GetParent(FrameworkElement child, Type targetType) {

            object parent = child.Parent;
            if (parent != null) {
                if (parent.GetType() == targetType) {
                    return (FrameworkElement)parent;
                } else {
                    return GetParent((FrameworkElement)parent, targetType);
                }
            }
            return null;
        }

这样便会对点名条件的就元格进行得操作了。

 相关材料:

http://blog.csdn.net/jhqin/article/details/7645357/

相关文章