Welcome to the Xceed Community Sign in | Join | Help
Community Search  

Re: Changing a column's EditTemplate based on the value of another column

  •  07-21-2008, 3:02 AM

    Re: Changing a column's EditTemplate based on the value of another column

    Hi Dustyn,

    As far as making a cell enabled or disabled (ReadOnly) at runtime, the following example will definitely suit your requirements. But to change a cell's EditTemplate at runtime does not seems to be an easy task as EditTemplate property belongs to the entire column and not just a single cell.

    Anywayz have a look at the following example that is written purely for your need. Do let me know if it helped or not.

            <xcdg:DataGridControl
                x:Name="myGrid">
                <xcdg:DataGridControl.Columns>
                    <xcdg:Column FieldName="Shade" Title="Shade of My Car"/>
                    <xcdg:Column FieldName="Tyres" Title="Tyres Supported" CellEditorDisplayConditions="Always">
                        <xcdg:Column.CellEditor>
                            <xcdg:CellEditor>
                                <xcdg:CellEditor.EditTemplate>
                                    <DataTemplate>
                                        <ComboBox
                                            ItemsSource="{xcdg:CellEditorBinding}"
                                            SelectedIndex="0"
                                            SelectionChanged="ComboBox_SelectionChanged"
                                        />
                                    </DataTemplate>
                                </xcdg:CellEditor.EditTemplate>
                            </xcdg:CellEditor>
                        </xcdg:Column.CellEditor>
                    </xcdg:Column>
                    <xcdg:Column FieldName="Cost" Title="Price"/>
                </xcdg:DataGridControl.Columns>
            </xcdg:DataGridControl>

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        
    ComboBox cb = (ComboBox)sender;
         Xceed.Wpf.DataGrid.
    DataRow row = GetDataRow(sender);
        
    if (row != null)
         {
             
    if (cb.SelectedIndex == 1)//If the selected tyre is "ALPHA"
             
    {
                   row.Cells[
    "Cost"].Background = new SolidColorBrush(Colors.Gray);
                   row.Cells[
    "Cost"].ReadOnly = true;
              }
              
    else //Anything else
              
    {
                   row.Cells[
    "Cost"].Background = new SolidColorBrush(Colors.Transparent);
                   row.Cells[
    "Cost"].ReadOnly = false;
              }
         }
    }

    public Xceed.Wpf.DataGrid.DataRow GetDataRow(object obj)
    {
         Xceed.Wpf.DataGrid.
    DataRow retVal = null; // if not found, return null
        
    try
        
    {
             
    if (obj is DependencyObject)// Must be a DependencyObject
             
    {
                  
    DependencyObject dobj = (DependencyObject)obj;
                  
    if (dobj is Xceed.Wpf.DataGrid.DataRow)// if we have a DataRow, return it.
                   
    {
                        retVal = (Xceed.Wpf.DataGrid.
    DataRow)dobj;
                   }
                  
    else
                  
    {
                        // Check the parant since this isn't a DataRow
                       
    DependencyObject parent = VisualTreeHelper.GetParent(dobj);
                       
    if (parent != null)
                        {
                             retVal =
    this.GetDataRow(parent);// if parent exist, call itself with parent.
                        
    }
                   }
               }
          }
          catch (Exception ex)
          {
              System.Diagnostics.
    Debug.Print(ex.Message);
          }
         
    return retVal;
    }

     

    Regards, 


    Abdullah Ansari
    Senior Software Engineer
    Outworks Solutions Private Limited

    Everything is okay in the end. If its not okay, then its not the end.
View Complete Thread
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.