编译与绑定相关的Listview XAML代码中的错误

时间:2012-02-12 14:15:22

标签: c# wpf xaml listview binding

我在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}">

我得到以下编译错误:

  

错误1 MouseDoubleClick =“{Binding Path = EditSelectedItemCommand}”是   无效。 '{Binding Path = EditSelectedItemCommand}'无效   事件处理程序方法名称只生成或生成实例方法   代码隐藏类是有效的。 12号线位置   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;

我在viewmodel中定义了绑定本身:

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

有人可以帮我解决编译错误吗?

提前致谢。

2 个答案:

答案 0 :(得分:2)

您不能绑定事件,只能绑定命令,您需要指定在代码隐藏中定义的方法名称作为错误注释。

e.g。

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

如果必须使用命令,则可以使用Interactivity之类的某些库(来自Blend SDK),这些库允许您在触发事件时执行命令。 e.g。

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

答案 1 :(得分:2)

为了将双击事件绑定到命令,您需要使用Blend的交互触发器,如下所述: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; }
}