Home Forums WPF controls Xceed DataGrid for WPF Button in Column of TableView

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #25176 |

    How do I display a button in every row of a TableView? When the user clicks the button, some code will run on the SelectedItem.

    Brian

    Imported from legacy forums. Posted by Brian (had 4281 views)

    User (Old forums)
    Member
    Post count: 23064

    You would have to redo the template of the Cell and add a button. For template examples you can look at :

    C:\Program Files\Xceed Components\Xceed DataGrid for WPF 1.2\Themes\Common for a common Template that will help you get started.

    Imported from legacy forums. Posted by Matt (had 641 views)

    User (Old forums)
    Member
    Post count: 23064

    Have managed to create a template with a button in it,  I was wondering how you can determine which row the button is in when you click on it, as it seems the button’s parent is null, as it won’t necessarily be the grid control’s ‘CurrentItem’.

    Imported from legacy forums. Posted by Jack (had 334 views)

    User (Old forums)
    Member
    Post count: 23064

    The data context of the current cell is the row source

    Imported from legacy forums. Posted by MiddleTommy (had 475 views)

    User (Old forums)
    Member
    Post count: 23064

    <quote>The data context of the current cell is the row source</quote>

     
    Sure, but there doesn’t seem to be a current cell?
     
    In my xaml I have:
     
    <DataTemplate x:Key=”emptyTemplate”>
     <Button x:Name=”btnEmpty” Content=”EMPTY” Click=”btnEmpty_Click”/>
            <DataTemplate.Triggers>
      <DataTrigger Binding=”{Binding}” Value=”False”>
                     <DataTrigger.Setters>
                             <Setter TargetName=”btnEmpty” Property=”IsEnabled” Value=”{Binding}” />
                      </DataTrigger.Setters>
      </DataTrigger>
      <DataTrigger Binding=”{Binding}” Value=”True”>
       <DataTrigger.Setters>
        <Setter TargetName=”btnEmpty” Property=”IsEnabled” Value=”{Binding}” />
       </DataTrigger.Setters>
      </DataTrigger>
     </DataTemplate.Triggers>
    </DataTemplate>
     
    and then further on:
     
    <xcdg:Column Title=”Empty” FieldName=”ExitGateOpen” MinWidth=”40″ MaxWidth=”40″ CellContentTemplate=”{StaticResource emptyTemplate}” />
     
    I then have in the code behind:
     
    private void btnEmpty_Click(object sender, RoutedEventArgs e)
    {
     // TODO: find the containing row
    }
     
    And I am trying to work out what row the button has been clicked for?  Any suggestions would be appreciated.

    Imported from legacy forums. Posted by Jack (had 3787 views)

    User (Old forums)
    Member
    Post count: 23064

    Here is something I did with a combobox as a work around (I hijacked the Tag property of the Button)

     <xcdg:CellEditor x:Key=”GradeCellEditor”>
            <xcdg:CellEditor.EditTemplate>
                <DataTemplate>
                    <ComboBox BorderThickness=”0″
                          BorderBrush=”Transparent”
                          VerticalContentAlignment=”Top”
                          SelectedValuePath=”Grade”
                          DisplayMemberPath=”Grade”
                          IsEditable=”True”
                          Text=”{Binding RelativeSource={RelativeSource FindAncestor,
                                                                AncestorType=xcdg:Cell},
                                                                Path=ActualContent,
                                                                UpdateSourceTrigger=PropertyChanged}”
                          GotFocus=”GradeComboBox_GotFocus”
                          Tag=”{Binding RelativeSource={RelativeSource FindAncestor,
                                                                AncestorType=xcdg:Cell},
                                                                UpdateSourceTrigger=PropertyChanged}”>
                    </ComboBox>
                </DataTemplate>
            </xcdg:CellEditor.EditTemplate>
            <xcdg:CellEditor.ActivationGestures>
                <xcdg:TextInputActivationGesture/>
            </xcdg:CellEditor.ActivationGestures>
        </xcdg:CellEditor>

     

    ……………………………………………………………………. 

    private void GradeComboBox_GotFocus(object sender, RoutedEventArgs e)
            {
                ComboBox cb = sender as ComboBox;
                if (cb != null)
                {
                    //Xceed.Wpf.DataGrid.DataRow dr; dr.
                    if (cb.ItemsSource == null)
                    {
                        DataCell ms = cb.Tag as DataCell;
                        if (ms != null)
                        {
                            ISize g = ms.DataContext as ISize;
                            if (g != null)
                            {
                                if (g.size.HasValue() && g.type.HasValue())
                                    cb.ItemsSource = InitGrades(g.type, g.ProcessSize().size);
                                else if (g.type.HasValue())
                                    cb.ItemsSource = InitGrades(g.type);
                            }
                            else
                            {
                                IMType ty = ms.DataContext as IMType;
                                if (ty != null)
                                    cb.ItemsSource = InitGrades(ty.type);
                            }
                        }
                    }
                }
            }

    Imported from legacy forums. Posted by MiddleTommy (had 914 views)

    Xceed Support
    Member
    Post count: 5658

    The DataGridContext is accessible throughout the VisualTree.

    You can therefore do this:

    DataGridContext dataGridContext = DataGridControl.GetDataGridContext( sender );

    //Here there is a small trick, get the “data item” for the container
    object item = dataGridContext.GetItemFromContainer( sender );

    //And re-ask for the container for the given data item, its gonna be the Row instance
    DataRow row = dataGridContext.GetContainerFromItem( item );

    DataCell cell = row.Cells[ dataGridContext.CurrentColumn ];

    While this procedure seems complex for no purposes, these methods are actually quite efficient and might prove simpler than navigating through the visual tree ( which is the only alternate method I can think of).

    Imported from legacy forums. Posted by Marcus [Xceed] (had 491 views)

    User (Old forums)
    Member
    Post count: 23064

    I tried those methods back in v1.0 of the grid but had problems with them working correctly.

    That is when I did the Tag hijack.

    Perhaps in v2.* they are all fixed and bug free. I just never re-checked 

    Imported from legacy forums. Posted by MiddleTommy (had 667 views)

    Xceed Support
    Member
    Post count: 5658

    They should be working…

    In fact, even in 1.0, it should have worked (with a slightly different method however).

    If you have any problems with this approach, please let us know and we’ll investigate further.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 258 views)

    User (Old forums)
    Member
    Post count: 23064

    Perhaps they did work in 1.0(and I noticed you have added new methods in later releases)

    I just didnt spend alot of time pursuing my troubles because I found a quick simple workaround that didn’t cause a visible performance decrease
     

    Imported from legacy forums. Posted by MiddleTommy (had 658 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks Marcus,

    That was exactly what I needed 🙂

    Imported from legacy forums. Posted by Jack (had 458 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Marcus,

     

    I am currently working with Datagrid for WPF 1.4 version, i need to implement the same, on click of button I need to open a modal window based on the rows values, I tried to use DataGridContext but I cant find it in Xceed.WPF.Datagrid namespace, am I missing something out here.

     2nd, I have checkbox in one of the columns, but for that i need to mark the already checked cells as readonly, how will i be able to access that particular cell while binding it through code behind.

     

    Thanks in Advance.

    Imported from legacy forums. Posted by Aarti (had 437 views)

    Xceed Support
    Member
    Post count: 5658

    DataGridContext is a new concept in version 2.0.

    In version 1.4, you could achieve the same result by using the DataGridControl.GetItemFromContainer() and DataGridControl.GetContainerFromItem() methods.

    As for your second question, maybe you could derive from DataRow and override the EndEdit() method to execute the code to set the appropriate cell to ReadOnly.

    Note: You should also override DataCell.Initialize() so that the ReadOnly status is cleared. This is because DataCell instances will be reused in other rows when they are not used anymore (scrolled out of view).

    For more details on how to override DataRow and DataCell, you can refer to this post. To use you custom DataCell type, override Row.CreateCell().

    Imported from legacy forums. Posted by Marcus [Xceed] (had 1595 views)

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.