Home > Enterprise >  Telerik WPF RadGridView DataTemplate button binding
Telerik WPF RadGridView DataTemplate button binding

Time:09-14

I want to bind a Button click event to my viewModel

Currently I have the following.

 <UserControl.Resources>
        <vm:PlannerOverviewViewModel x:Key="ViewModel"/>
    </UserControl.Resources>

 <telerik:RadGridView x:Name="ViewResponsibleOrganisationUnits" Grid.Row="1" ShowGroupPanel="False" CanUserFreezeColumns="False" FrozenColumnsSplitterVisibility="Collapsed"
                                                    CanUserDeleteRows="True" CanUserInsertRows="True" CanUserReorderColumns="True" BorderThickness="0"
                                                    CanUserResizeColumns="True" CanUserResizeRows="False" CanUserSortColumns="True"
                                                    CanUserSortGroups="False" ItemsSource="{Binding Path=ResponsibleForOrganisationUnits}" AutoGenerateColumns="False"  CurrentItem="{Binding Path=ResponsibleForOrganisationUnit}"
                                                    SelectionMode="Extended" SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="True"
                                                    IsReadOnly="false" ShowColumnFooters="True" RowDetailsVisibilityMode="Collapsed" FilteringMode="Popup"
                                                    IsFilteringAllowed="False" GridLinesVisibility="Both" 
                                                    ShowSearchPanel="False" GroupRenderMode="Flat" EnableStickyGroupHeaders="False" AutoExpandGroups="False" RowHeight="22" NewRowPosition="Bottom">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="RowEditEnded">
                                            <i:InvokeCommandAction Command="{Binding EditOrganisationUnitForPlannerCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=xRadGridView, Path=SelectedItem}"/>
                                        </i:EventTrigger>
                                        <i:EventTrigger EventName="AddingNewDataItem">
                                            <i:InvokeCommandAction Command="{Binding AddOrganisationUnitForPlannerCommand, Mode=OneWay}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <!-- DOUBLE CLICK -->
                                    <!--<telerik:EventToCommandBehavior.EventBindings>
                                <telerik:EventBinding Command="{Binding Path=ItemDoubleClickCommand, Mode=OneTime}" EventName="MouseDoubleClick"/>
                            </telerik:EventToCommandBehavior.EventBindings>-->
                                    <telerik:RadGridView.FilterDescriptors>
                                        <telerik:FilterDescriptor Member="Code"/>
                                    </telerik:RadGridView.FilterDescriptors>
                                    <!-- COLUMNS -->
                                    <telerik:RadGridView.Columns>
                                        <telerik:GridViewSelectColumn UniqueName="COL_SELECTOR" />                                                
                                        <telerik:GridViewDataColumn Header="{Binding Path=Translations.PlannerOverviewView_Grid_OrganisationUnitId}" DataMemberBinding="{Binding OrganisationUnitId}" Width="100" UniqueName="COL_ORGANISATIONUNITID">
                                            <telerik:GridViewDataColumn.AggregateFunctions>
                                                <telerik:CountFunction ResultFormatString="{}{0:0.##} Records" />
                                            </telerik:GridViewDataColumn.AggregateFunctions>
                                        </telerik:GridViewDataColumn>
                                        <telerik:GridViewDataColumn Header="{Binding Path=Translations.PlannerOverviewView_GridOrganisationUnit_Description}" DataMemberBinding="{Binding Description}" Width="210" UniqueName="COL_ORGANISATIONUNIT"/>
                                        <telerik:GridViewDataColumn Header="{Binding Path=Translations.PlannerOverviewView_GridOrganisationUnit_Organisation}" DataMemberBinding="{Binding Organisation}" Width="*" UniqueName="COL_EMPLOYEE_NAME"/>
                                        <telerik:GridViewDataColumn Header="{Binding Path=Translations.PlannerOverviewView_GridOrganisationUnit_Management}" DataMemberBinding="{Binding Management}" Width="*" UniqueName="COL_EMPLOYEE_AMEI"/>
                                        <telerik:GridViewDataColumn Header="{Binding Path=Translations.PlannerOverviewView_GridOrganisationUnit_Department}" DataMemberBinding="{Binding Department}" Width="*" UniqueName="COL_EMPLOYEE_ABBREVIATION"/>
                                        <telerik:GridViewDataColumn Header="{Binding Path=Translations.PlannerOverviewView_GridOrganisationUnit_Section}" DataMemberBinding="{Binding Section}" Width="80" UniqueName="COL_ISACTIVE"/>                                        
                                        <telerik:GridViewDataColumn Header="{Binding Path=Translations.PlannerOverviewView_GridOrganisationUnit_Type}" DataMemberBinding="{Binding Type}" Width="*" UniqueName="COL_EMPLOYEE_NAME"/>
                                        <telerik:GridViewColumn Width="40">
                                            <telerik:GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <telerik:RadButton x:Name="BtnDeleteOrganinsationUnit" Command="{Binding Path=DeleteOrganisationUnitCommand, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type vm:PlannerOverviewViewModel}} }">
                                                        <StackPanel Orientation="Horizontal" Margin="0,0">
                                                            <mahapps:PackIconMaterial Kind="CloseBox" HorizontalAlignment="Center" VerticalAlignment="Center" Width="14" Height="14" Foreground="{StaticResource ArcelorAccentBrush}"/>
                                                        </StackPanel>
                                                    </telerik:RadButton>
                                                </DataTemplate>
                                            </telerik:GridViewColumn.CellTemplate>
                                        </telerik:GridViewColumn>
                                    </telerik:RadGridView.Columns>
                                </telerik:RadGridView>

The binding to the viewmodel is not working Command="{Binding Path=DeleteOrganisationUnitCommand, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type vm:PlannerOverviewViewModel}} }"

ViewModel

   public IDelegateCommand DeleteOrganisationUnitCommand
        {
            get
            {
                return deleteOrganisationUnitCommand ?? (deleteOrganisationUnitCommand = DelegateCommand.CreateAsync(ExecuteDeleteOrganisationUnitCommandAsync, CanExecuteDeleteOrganisationUnitCommand));
            }
        }

Any help would be appriciated

CodePudding user response:

In your case you can do this:

Command="{Binding Path=DeleteOrganisationUnitCommand, Source={StaticResource ViewModel}, Mode=OneWay}"

CodePudding user response:

PlannerOverviewViewModel is not a visual - but RadGridView is.

The following should work if DeleteOrganisationUnitCommand is a property of the DataContext of the RadGridView, i.e. if DeleteOrganisationUnitCommand is defined in the same class as ResponsibleForOrganisationUnits:

Command="{Binding Path=DataContext.DeleteOrganisationUnitCommand,
    RelativeSource={RelativeSource AncestorType={x:Type telerik:RadGridView}} }"
  • Related