2012-08-01 88 views
0

我有一個Item控件,它由一個列表填充,而List是兩個參數'Time'和'Description'的集合。對於它,我使用HyperLinkBut​​ton進行時間和標籤的描述。ItemControl中的事件觸發器不工作

我想要的是,我想使用Main viewModel中的超鏈接按鈕的EventTrigger創建點擊事件。我的代碼是:

<ItemsControl 
    x:Name="transcriptionTextControl" 
    ItemsSource="{Binding MyCollectionOfTranscription, Mode=TwoWay}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <HyperlinkButton Content="{Binding Time}"> 
        <ToolTipService.ToolTip> 
         <ToolTip Content="Time"/> 
        </ToolTipService.ToolTip> 
        <i:Interaction.Triggers> 
         <i:EventTrigger EventName="Click"> 
          <i:InvokeCommandAction 
           Command="{Binding HyperLinkButtonCommand}" 
           CommandParameter="{Binding 
            ElementName=transcriptionTextControl }" /> 
         </i:EventTrigger> 
        </i:Interaction.Triggers> 
       </HyperlinkButton> 
       <sdk:Label Content="{Binding Description}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

當我建立的項目,它不會給錯誤,但爲的ICommand超鏈接,顯示爲「無法解析符號HyperLinkBut​​tonCommand」,同時此事件觸發超出此工作的罰款警告。

沒有得到,什麼是它背後的實際問題,PLZ給您的寶貴建議......

+2

爲什麼你使用觸發器呢? HyperlinkBut​​ton已經具有Command屬性。 – 2012-08-01 11:14:56

回答

1

首先,

<i:InvokeCommandAction 
    Command="{Binding HyperLinkButtonCommand}" 
    CommandParameter="{Binding 
     ElementName=transcriptionTextControl }" /> 

正試圖以實例找到一個名爲HyperLinkButtonCommand屬性上的綁定包含在MyCollectionOfTranscription中的類型(您不需要綁定到這個雙向)。

(側面說明,發送一個ItemsControl到您的命令是 MVVM)

ItemsControl遍歷這個集合中的每個元素,創造了ItemsControl.ItemTemplate定義的模板的副本,並設置BindingContext等於到這個元素(我假設它是一個腳本)。如果您啓動了數據綁定調試設置,您可以從綁定中獲得的警告中找出無法找到您的HyperLinkButtonCommand的警告。

enter image description here

假設

  1. HyperLinkButtonCommand是在你的視圖模型定義的命令,並
  2. 此XAML的根是一個Window(可能是一個用戶控件,但我在此假設)
  3. 您的ViewModel是窗口的DataContext

您可以更改綁定到下面的,它應該工作(或者你應該從它那裏得到一個線索)

<i:InvokeCommandAction 
    Command="{Binding HyperLinkButtonCommand, 
       RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" 
    CommandParameter="{Binding 
     ElementName=transcriptionTextControl }" /> 

我寧願只給我的根的「根」的x:Name和使用「的ElementName =根「在這種情況下。

+0

現在我的代碼沒有發出警告,但hyperLinkBut​​tonCommand沒有觸發。 – 2012-08-03 09:14:59

+0

讓我詳細解釋一下。我爲ViewModel中的HyperLinkBut​​tonCommand屬性設置了代碼: HyperLinkBut​​tonCommand = new DelegateCommand(HyperLinkBut​​tonAction,CanHyperLinkBut​​ton); public ICommand HyperLinkBut​​tonCommand {get;組; } public void HyperLinkBut​​tonAction(object param) { MessageBox。顯示( 「HII」); } private bool CanHyperLinkBut​​ton(object param) { return true; } 並且在時間和描述字段中會出現重複,所以我想在每次單擊「時間」超鏈接按鈕時顯示messageBox,但它不會觸發。 – 2012-08-03 09:27:36

+0

@ user1298386:編輯您的問題並添加實例化您的命令的方法。 – Will 2012-08-03 12:31:41