前几天,公司同事有个需求需要对系统中的DataGrid控件的指定单元格(如图,申请人ID)进行禁用设置,尝试了很多次总是 整行整列的 禁用 没实现效果。 网上资料较少,没找到解决措施。
尽管silverlight的datagrid控件和 asp.net的名字相同,但很多方面还是不大一样的。
但是换个思维方式, 任何事务都是由对象 构成,Asp.net中的datagrid的单元格是cell对象,所以 silverlight的单元格 应该也是 一个对象,一种类型,可以通过一些方法被获取到。
多次尝试后通过以下的 代码方案简单明了的 找到了 目标条件的 单元格 DataGridCell对象。
前端xaml代码:
1 <sdk:DataGrid x:Name="dataGrid" SelectionMode="Single" Grid.Row="1" Margin="-1,-1,-1,0" CopyingRowClipboardContent="dataGrid_CopyingRowClipboardContent"View Code
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>
需要设置dataGrid_BeginningEdit事件。
后端代码:
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/