4

I am using a style with button in a radgridview header as HeaderCellStyle.Button is placed properly but command is not invoking

Below is my code:

<telerik:GridViewImageColumn Header="" 
                                         Width="30"
                                         ImageHeight="30" 
                                         IsResizable="False" 
                                         DataMemberBinding="{Binding Image}"                                             
                                         HeaderCellStyle="{StaticResource ButtonStyle}"  >
              </telerik:GridViewImageColumn>

Button style:

<Style TargetType="telerik:GridViewHeaderCell" x:Key="ButtonStyle">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="telerik:GridViewHeaderCell">
        <telerik:RadButton x:Name="ClearButton"  Content="{Binding ClearButton,Source={StaticResource FrameworkInfrastructureResources}}"
                           ToolTip="{Binding ClearTooltip,Source={StaticResource FrameworkInfrastructureResources}}"  Margin="5"
                           IsEnabled="True" 
                           HorizontalContentAlignment="Center" 
                           Command="{Binding ClearMessagesCommand}"
        </telerik:RadButton>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

Below is my command in viewmodel:

  public ICommand ClearMessagesCommand { get; set; }
  ClearMessagesCommand = new DelegateCommand(() => { this.Messages.Clear(); });

1 Answer 1

4

You can use RelativeSource for binding to datacontext of your cell:

<telerik:RadButton x:Name="ClearButton"  Content="{Binding ClearButton,Source={StaticResource FrameworkInfrastructureResources}}"
                       ToolTip="{Binding ClearTooltip,Source={StaticResource FrameworkInfrastructureResources}}"  Margin="5"
                       IsEnabled="True" 
                       HorizontalContentAlignment="Center" 
                       Command="{Binding Path=DataContext.ClearMessagesCommand,,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type telerik:GridViewHeaderCell}}}"
</telerik:RadButton>

There is detailed SO question about RelativeSource. You can also try to bind via ElementName.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.