2016-11-11 52 views
0

我想让我的DataGridCheckBoxColumn.Header是一个按钮,并且一旦点击就运行一个命令。使用ViewModel中的DelegateCommand删除检查的实体。WPF DataGridHeaderTemplate作为按钮

 <DataGrid ItemsSource="{Binding EmployeeCollection}" RowDetailsVisibilityMode="VisibleWhenSelected" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="False" > 
      <DataGrid.Columns> 
       <DataGridCheckBoxColumn CanUserSort="False" IsReadOnly="False" Binding="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" > 
        <DataGridCheckBoxColumn.HeaderTemplate> 
         <DataTemplate> 
          <Button Command="{Binding DeleteCommand}"> 
           <StackPanel> 
            <Image Source="../Images/SearchIcon.jpg" /> 
           </StackPanel> 
          </Button> 

         </DataTemplate> 
        </DataGridCheckBoxColumn.HeaderTemplate> 
       </DataGridCheckBoxColumn> 

     </DataGrid> 

有谁知道为什么这不起作用?当你点击标题时,我也禁用了列排序,希望它能运行我的按钮。 也许标题是一个容器,我的按钮隐藏在后面?

+0

寻求调试帮助的问题(“为什么这个代码不工作?”)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者无益。请参阅:[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve) – ASh

+0

请参阅我的更新回答。 – AnjumSKhan

回答

-1

试试这个:

<DataGrid.Resources> 
    <DiscreteObjectKeyFrame x:Key="ProxyKey" Value="{Binding}"/> 
</DataGrid.Resources> 
... 
<Button Command="{Binding Value.DeleteCommand, Source={StaticResource ProxyKey}}"> 

看看这能解决你的问题。