RadButton Command in GridViewColumn doesn't Work sometimes

6 posts, 0 answers
  1. Frank
    Frank avatar
    2 posts
    Member since:
    Jul 2012

    Posted 26 Jul 2012 Link to this post

    Dear telerik,

    I have to fix a problem in our software and we are using a gridview, which contains articles as items.
    Each Item has a deletebutton (only visible for the the selected one), which Command is bound on a DelegateCommand (in the ViewModel). We are using the MVVM-pattern.
    This button is only for removing the selected item of the gridview and works fine but when i have more than 16 articles in the grid there is always one or two articles, which I can't delete with the button. The other ones work fine.

    The routed method doesn't get called in this times and I don't know why. A Button Click Event always works even when the bound command doesn't work.

    I don't know where I have to search this mistake and would like to use the command and not the buttonclick.

    Did you ever hear of a problem like this? Could you please help me?

    Thanks a lot.


    Cheers

    Frank  
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 27 Jul 2012 Link to this post

    Hi Frank,

    The RadGridView exposes a Delete command (RadGridViewCommands.Delete) which you can use to implement your scenario. It is specifically designed to allow you to delete rows of data.  You can examine the RadGridView Commands example in our WPF demos - http://demos.telerik.com/wpf/#GridView/Commands.

    Regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Frank
    Frank avatar
    2 posts
    Member since:
    Jul 2012

    Posted 30 Jul 2012 Link to this post

    Thanks a lot for your quick answer.

    I tried to implement your approach but this command is not reachable at this place.
    I am afraid that my post wasn't good enough so I took 3 screens to descripe my problem better.

    pic1 shows that I can't reach the RadGridViewCommands.Delete, I guess this is beacause of being in a column, isn't it?

    pic2 and pic3 show how I implemented the Grid and that the Items hold the deletebuttons (RadButton in a column) .

    My current Binding looks like this:
    <Command="{Binding DataContext.DeleteLineCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:GridViewDataControl}}}"> 

    This solution works mostly but when I have more than 16 items in the grid there are one or two Items where the button won't work.
    The getter of this command, in this times, isn't get called and the method don't starts.

    Thanks for helping
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 30 Jul 2012 Link to this post

    Hi Frank,

    Thank you for getting back to us. It's really better to change your approach to use the RadGridViewCommands.Delete command as it's especially designed for such scenarios. 

    Please note that in order to use it, you need to apply it on the RadButton Command property and your screenshot indicates that you're trying to use it as an attached property in the RadButton definition. Instead it should be used like this:

    ...
    xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    ...
    <telerik:RadGridView x:Name="RadGridView1"
                        ...>
        <telerik:RadGridView.Columns>
                ... 
            <telerik:GridViewColumn Width="90">
                <telerik:GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <telerik:RadButton Content="Delete"
                    Command="telerikGrid:RadGridViewCommands.Delete"
                    CommandParameter="{Binding}" />
                    </DataTemplate>
                </telerik:GridViewColumn.CellTemplate>
            </telerik:GridViewColumn>
            ...
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>

    This approach shouldn't cause any issues when deleting rows. If you download our WPF demos and navigate to the GridView demo samples, you can open the Commands example in the Editing section. You can examine both the xaml and the code-behind implementation of the example and use the same approach in your application.

    Please let me know if it works.Regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Yaron
    Yaron avatar
    2 posts
    Member since:
    Aug 2013

    Posted 30 Sep 2013 Link to this post

    Hello team.

    I'm looking for the solution when button command will be binding to my command in the relevant ViewModel. I'm following the instruction but nothing works for me. Please advice.

    Here is my code:

                        <telerik:GridViewDataColumn Width="25">
                            <telerik:GridViewDataColumn.CellStyle>
                                <Style TargetType="telerik:GridViewCell">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="telerik:GridViewCell">
                                                <Button   
                                                Command="{Binding DataContext.SelectPatientCommand, ElementName=ingredients}" 
                                                HorizontalAlignment="Center"   
                                                VerticalAlignment="Center"
                                                    Content=">"/>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </telerik:GridViewDataColumn.CellStyle>
                        </telerik:GridViewDataColumn>

    where "ingredients" is a X:Name of the GridView
  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 02 Oct 2013 Link to this post

    Hi Frank,

    My recomendation would be to work with CellTemplate rather than changing the ControlTemplate of a cell. Considering invoking the command, DataContext of the cell is the data item corresponding to that row and if the command is defined in your ViewModel, it cannot be reached. What you can try is to set explicitly the Source property of the command binding:

    <Button  
                                                Command="{Binding SelectPatientCommand, Source={StaticResource MyViewModel}}"
                                                HorizontalAlignment="Center"  
                                                VerticalAlignment="Center"
                                                    Content=">"/>

     

    Regards,
    Maya
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top
UI for WPF is Visual Studio 2017 Ready