2015-12-21 50 views
3

我使用同一个radgridview标题为HeaderCellStyle.Button按钮的样式放置正确,但命令不调用如何绑定在WPF风格的按钮指令

下面是我的代码:

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

按钮风格:

<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> 

下面是我在视图模型命令:

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

回答

2

可以使用RelativeSource结合的datacontext您的电池:

<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> 

detailed SO question有关的RelativeSource。您也可以尝试通过ElementName进行绑定。