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

本文关键字:编译 错误 绑定 XAML 代码 Listview | 更新日期: 2023-09-27 18:24:17

我在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''KorgKroosTools''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()));
        }
    }

有人能帮我纠正编译错误吗?

提前谢谢。

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

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

例如

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>

为了将双击事件绑定到命令,您需要使用Blend的交互触发器,如下所述:WPF:如何使用MVVM将命令绑定到ListBoxItem?。

我的例子是:

<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; }
}