[Original] Silverlight DataGrid obtains and sets and analyzes any cell of the Core Control DataGrid ., Silverlightdatagrid

Source: Internet
Author: User

[Original] Silverlight DataGrid obtains and sets and analyzes any cell of the Core Control DataGrid ., Silverlightdatagrid

A few days ago, my colleagues in the company had a need to disable the specified cell (applicant ID) of the DataGrid Control in the system. They tried to disable the entire row many times without any effect. There is less information on the Internet and no solution is found.

Although the datagrid Control of silverlight has the same name as asp.net, it is not the same in many aspects.

However, in another way of thinking, any transaction is composed of objects. The cells in the datagrid in Asp.net are cell objects, so the cells in silverlight should also be an object and a type, some methods can be obtained.

 

After multiple attempts, the following code scheme is used to find the cell DataGridCell object of the target condition.

 

 

Front-end xaml code:

1 <sdk: DataGrid x: Name = "dataGrid" SelectionMode = "Single" Grid. row = "1" Margin = "-1,-1, -"CopyingRowClipboardContent =" regular "2 AutoGenerateColumns =" False "VerticalContentAlignment =" Stretch "Loaded =" dataGrid2_Loaded "3 rows =" Stretch "Grid. columnSpan = "2" CellEditEnded = "dataGrid_CellEditEnded" 4 ColumnHeaderStyle = "{StaticResource DataGridHeaderStyle} "LoadingRow =" maid "BeginningEdit =" maid "5 CurrentCellChanged =" maid "> 6 <sdk: DataGrid. columns> 7 <sdk: Maid Width = "100" Header = "query object"> 8 <sdk: maid. cellTemplate> 9 <DataTemplate> 10 <TextBlock Height = "20" Margin = "2, 0" verticalignment = "Center" HorizontalAlignment = "Left" 11 Text = "{Binding QueryType, Converter = {StaticResource QueryTypeConverter}, Mode = TwoWay} "/> 12 </DataTemplate> 13 </sdk: DataGridTemplateColumn. cellTemplate> 14 <sdk: maid. cellEditingTemplate> 15 <DataTemplate> 16 <ComboBox ItemsSource = "{Binding KeyNameList, Source = {StaticResource QueryTypeConverter}" 17 SelectedItem = "{Binding QueryType, Converter = {StaticResource QueryTypeConverter }, mode = TwoWay} "/> 18 </DataTemplate> 19 </sd K: DataGridTemplateColumn. cellEditingTemplate> 20 </sdk: DataGridTemplateColumn> 21 <sdk: dataGridTextColumn Width = "0.3 *" Header = "query Content" Binding = "{Binding Content}"/> 22 23 <sdk: dataGridTemplateColumn Width = "70" Header = "abnormal"> 24 <sdk: DataGridTemplateColumn. cellTemplate> 25 <DataTemplate> 26 <TextBlock Margin = "2, 0" verticalignment = "Center" HorizontalAlignment = "Left" 27 Text = "{Binding ResultDesc, Converter = {StaticResource IsResultDescConverter}, Mode = TwoWay} "/> 28 </DataTemplate> 29 </sdk: DataGridTemplateColumn. cellTemplate> 30 <sdk: maid. cellEditingTemplate> 31 <DataTemplate> 32 <ComboBox ItemsSource = "{Binding KeyNameList, Source = {StaticResource IsResultDescConverter}" 33 SelectedItem = "{Binding ResultDesc, Converter = {StaticResource detail }, mode = TwoWay} "34 Height =" 25 "/> 35 </DataTemplate> 36 </sdk: DataGridTemplateColumn. CellEditingTemplate> 37 </sdk: DataGridTemplateColumn> 38 <! -- <Sdk: DataGridTextColumn Width = "0.7 *" Header = "Remarks" Binding = "{Binding Path = Remark}"/> --> 39 <sdk: dataGridTemplateColumn Width = "0.7 *" Header = "Remarks"> 40 <sdk: DataGridTemplateColumn. cellTemplate> 41 <DataTemplate> 42 <TextBlock Text = "{Binding Remark}" TextWrapping = "Wrap" verticalignment = "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: Maid> 51 <sdk: Maid Width = "45" Header = "operation"> 52 <sdk: maid. cellTemplate> 53 <DataTemplate> 54 <StackPanel Orientation = "Horizontal" Height = "22"> 55 <Image x: Name = "btnAdd" ToolTipService. toolTip = "added" MouseLeftButtonUp = "Courier" Source = "/CTOP2P; component/Assets/Image/Action_LinkUnlink_Link.png" Style = "{StaticResource imgButton}"/> 56 <Image x: name = "btnCopy" Visibility = "Collapsed" ToolTipService. toolTip = "copy" MouseLeftButtonUp = "btnCopy_MouseLeftButtonUp" Source = "/CTOP2P; component/Assets/Image/Action_Copy.png" Style = "{StaticResource imgButton}"/> 57 <Image x: name = "btnDel" ToolTipService. toolTip = "delete" 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: Maid> 62 </sdk: DataGrid. columns> 63 </sdk: DataGrid>View Code

You need to set the datagriinningedit event.

Backend code:

Private void dataGrid_BeginningEdit (object sender, DataGridBeginningEditEventArgs e) {var item = e. row. dataContext as AuditQueryInfo; if (item. queryType. equals ("applicant 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) {maid cell = (maid) 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;        }

In this way, the cell with the specified condition can be obtained.

Related information:

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.