2012-02-12 18 views
0

我在XAML中定義的列表視圖,請參見下面的片段:編譯錯誤列表視圖XAML代碼與結合

<Grid> 
    <Button Content="_Generate List ..." Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" Name="buttonGenerateLists" 
      VerticalAlignment="Bottom" Click="ButtonGenerateListsClick" Width="108" Grid.Column="1" /> 
    <ListView HorizontalAlignment="Stretch" Margin="275,34,13,96" Name="listViewPatches" VerticalAlignment="Stretch" SelectionMode="Extended" 
       VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" AlternationCount="1" GotFocus="ListViewPatchGotFocus" 
       MouseDoubleClick="{Binding Path=EditSelectedItemCommand}" SelectedItem="{Binding IsSelected}"> 

我也得到了以下編譯錯誤:

Error 1 MouseDoubleClick="{Binding Path=EditSelectedItemCommand}" is not valid. '{Binding Path=EditSelectedItemCommand}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid. Line 12 Position 19. G:\Data\Eigen\Informatica\KorgKronosTools\KorgKronosTools\PcgWindow.xaml 12 19 PcgTools

(注:第12行是上面片段中的最後一行)。

我想我沒在下面的片段背後我的代碼設置數據上下文正確的,但是編碼:

public PcgWindow(MainWindow mainWindow, string pcgFileName, PcgMemory pcgMemory) 
{ 
    InitializeComponent(); 

    _mainWindow = mainWindow; 
    _viewModel = new PcgViewModel(mainWindow.ViewModel); 

    ... 


    DataContext = _viewModel; 

我定義的視圖模型綁定本身:

ICommand _editSelectedItemCommand; 
    public ICommand EditSelectedItemCommand 
    { 
     get 
     { 
      return _editSelectedItemCommand ?? (_editSelectedItemCommand = new RelayCommand(param => EditSelectedItem(), 
       param => CanExecuteEditSelectedItem())); 
     } 
    } 

有人可以幫我解決編譯錯誤嗎?

在此先感謝。

回答

2

您不能綁定事件,只有命令,您需要指定一個在代碼隱藏中定義的方法名稱作爲錯誤提示。

例如

MouseDoubleClick="OnMouseDoubleClick" 
private void OnMouseDoubleClick(object sender, RoutedEventArgs e) 
{ 
    // Do something 
} 

如果你必須使用一個命令,您可以使用特定的庫,例如Interactivity(從Blend SDK),它允許你當一個事件被觸發執行命令。例如

<ListView ...> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="MouseDoubleClick"> 
      <i:InvokeCommandAction Command="{Binding EditSelectedItemCommand}" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</ListView> 
+0

謝謝你,它的工作...我用的按鈕,工具欄等的命令,但這並不在列表視圖中,雙擊運行。 – 2012-02-12 14:26:49

+0

感謝您的澄清......我可能會使用該方法來清理代碼背後的代碼(並且我已經實現了該命令本身)。 – 2012-02-12 14:32:56

2

爲了將雙擊事件綁定到你需要使用如這裏討論混合的交互觸發命令:WPF: How to bind a command to the ListBoxItem using MVVM?

我的例子是:

<Grid> 
    <Grid.Resources> 
     <DataTemplate x:Key="CustomerTemplate" DataType="{x:Type ViewModel:Customer}"> 
      <ContentControl> 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="MouseDoubleClick"> 
         <i:InvokeCommandAction Command="{Binding DoubleClickCommand}" /> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
       <TextBlock Text="{Binding Name}"/> 
      </ContentControl> 
     </DataTemplate> 
    </Grid.Resources> 
    <ListBox ItemsSource="{Binding Customers}" ItemTemplate="{StaticResource CustomerTemplate}" /> 
</Grid> 


public class Customer : ViewModelBase 
{ 
    public Customer() 
    { 
     DoubleClickCommand = new RelayCommand(DoubleClick);  
    } 

    private void DoubleClick() 
    { 
     Debug.WriteLine("double click"); 
    } 

    private string _name; 

    public string Name 
    { 
     get { return _name; } 
     set { Set(() => Name, ref _name, value); } 
    } 

    public ICommand DoubleClickCommand { get; private set; } 
}