2014-01-10 48 views
2

我試圖將按鈕添加到XamDataPresenter中的未綁定字段。WPF綁定:將按鈕添加到網格中的未綁定字段

這裏是按鈕模板:

 <Style x:Key="CancelButtonTemplate" TargetType="{x:Type igDP:CellValuePresenter}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> 
         <Button x:Name="CancelButton" Content="Cancel" Command="{Binding CancelButtonCommand}" Width="80" Height="20" /> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

這裏是在XamDataPresenter未綁定字段:

    <igDP:UnboundField Name="CancelOrder" Label="Cancel Order" Column="11"> 
         <igDP:UnboundField.Settings> 
          <igDP:FieldSettings CellValuePresenterStyle="{StaticResource CancelButtonTemplate}" CellHeight="12" CellWidth="50"> 
          </igDP:FieldSettings> 
         </igDP:UnboundField.Settings> 
        </igDP:UnboundField>      
       </igDP:FieldLayout.Fields> 

的 「CancelButtonCommand」 的按鈕綁定在視圖模型中是公共財產,我已經驗證它的工作原理在XamDataPresenter之外有一個按鈕,並且沒有模板。

該按鈕顯示在網格中,但沒有任何反應,當我按下它。

我到底做錯了什麼?

+0

binding.how有什麼問題糾正嗎? –

+0

您是否嘗試過我的建議?像我的答案一樣修改你的XAML,看看它是否有效,它應該基本上告訴命令要採取什麼行動。 –

+1

感謝您的回覆,問題是由於綁定only.I改變了路徑&它的工作:

回答

1

我相信你需要添加一點到你的風格綁定。嘗試更改爲:

<Button Command="{Binding DataContext.CancelButtonCommand, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}" 
           CommandParameter="{Binding}"> 

您是使用XamDataPresenter還是XamDataGrid?如果演示者,更改上面的x:類型爲XamDataPresenter而不是XamDataGrid

此外:如果我記得,我不得不添加CommandParameter,因此該命令將知道哪個行將採取行動,我需要該方法內的SelectedItem。否則,每個按鈕的行爲將完全相同。在我的情況下,每個按鈕應該做些什麼來排它是在對象

+1

謝謝爲提及selecteditem ..但在我的情況下,每個項目將刪除selecteditem,所以它的工作如需要 –

1

使用按鈕,如下(元件結合):

<Button x:Name="CancelButton" Content="Cancel" Command="{Binding ElementName=theotherbuttonworkingthiscommand,Path=DataContext.CancelButtonCommand}" Width="80" Height="20" /> 

或使用相對源綁定

<Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type window}},Path=DataContext.CancelButtonCommand}">