Welcome to the Xceed Community | Help
Community Search  

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

Sort Posts: Previous Next
  •  07-18-2008, 9:49 AM Post no. 13617

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

    Someone please help ... 

    My scenario is that if the user chooses a particular value (combobox) in one column of the grid, another column must either allow him to select further values or it becomes "not applicable". The closest solution I've managed to find was here http://pstaev.blogspot.com/2008/06/having-different-celleditor-for.html. The problem is that in that example, Peter used the value of the current cell in his DataTrigger. My logic depends on a different column. I've tried to access that value using CurrentItem ... but because the row user is still on the same row, it hasn't been commited and you simply get the old value.

  •  07-21-2008, 3:02 AM Post no. 13646 in reply to 13617

    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.
  •  07-21-2008, 11:03 AM Post no. 13655 in reply to 13646

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

    Hi Abdullah,

    Many thanks for the response. That approach helped me quite a lot. I also found that if you update the row's data context in the "ComboBox_SelectionChanged" event handler, that you can then use data triggers in XAML because they then use then new value rather than waiting for the EndEdit command to be called.

    Regards,
    Dustyn 

     

     

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.