簡體   English   中英

如何以wpf樣式綁定按鈕命令

[英]How to bind a button command in wpf style

因為HeaderCellStyle.Button正確放置但是命令沒有調用,我在radgridview頭中使用帶按鈕的樣式

以下是我的代碼:

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

以下是我在viewmodel中的命令:

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

您可以使用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>

關於RelativeSource有詳細的SO問題 您還可以嘗試通過ElementName綁定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM