Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么不能将命令绑定到双击listbox命令_C#_Wpf_Mvvm - Fatal编程技术网

C# 为什么不能将命令绑定到双击listbox命令

C# 为什么不能将命令绑定到双击listbox命令,c#,wpf,mvvm,C#,Wpf,Mvvm,我试图将一个数字列表绑定到一个列表框,并设置doubleClickCommand,这样当我双击一个项目时,它将运行SetItem方法 我的看法 <Grid> <StackPanel> <TextBlock Text="{Binding Item}"/> <ListBox ItemsSource="{Binding List}" Height="515"

我试图将一个数字列表绑定到一个列表框,并设置doubleClickCommand,这样当我双击一个项目时,它将运行SetItem方法

我的看法

 <Grid>
    <StackPanel>
        <TextBlock Text="{Binding Item}"/>
        <ListBox ItemsSource="{Binding List}" Height="515" SelectedItem="{Binding SelectedItem}" Grid.Column="0">
            <ListBoxItem>
                <ListBoxItem.InputBindings>
                    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding Command}" />
                </ListBoxItem.InputBindings>
            </ListBoxItem>
        </ListBox>
    </StackPanel>
</Grid>

还有我的ViewModel

     public class MainWindowViewModel : BindableBase
{
    public DelegateCommand Command { get; private set; }

    private string _title = "Prism Application";
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    public MainWindowViewModel()
    {
        Command = new DelegateCommand(SetItem);
        List = new List<string>();
        List.Add("one");
        List.Add("two");
        List.Add("three");
    }

    private void SetItem()
    {
        Item = SelectedItem;
    }

    private string _item;
    public string Item
    {
        get { return _item; }
        set { SetProperty(ref _item, value); }
    }

    private string _selectedItem;
    public string SelectedItem
    {
        get { return _selectedItem; }
        set { SetProperty(ref _selectedItem, value); }
    }

    private List<string> _list;
    public List<string> List
    {
        get { return _list; }
        set { SetProperty(ref _list, value); }
    }
}
公共类MainWindowViewModel:BindableBase { 公共DelegateCommand命令{get;private set;} 私有字符串_title=“Prism应用程序”; 公共字符串标题 { 获取{return\u title;} 集合{SetProperty(ref _title,value);} } 公共主窗口视图模型() { Command=新的DelegateCommand(SetItem); 列表=新列表(); 列表。添加(“一”); 列表。添加(“两个”); 列表。添加(“三”); } 私有void SetItem() { Item=SelectedItem; } 私有字符串_项; 公共字符串项 { 获取{return\u item;} 集合{SetProperty(ref _项,值);} } 私有字符串_selectedItem; 公共字符串SelectedItem { 获取{return\u selectedItem;} set{SetProperty(ref _selectedItem,value);} } 私人名单(private List);; 公开名单 { 获取{return\u list;} set{SetProperty(ref_list,value);} } } 当我试图运行代码时,我得到了这个异常

InvalidOperationException:在使用ItemsSource时,操作无效。改为使用ItemsControl.ItemsSource访问和修改元素


有没有办法解决这个问题;或者我需要以其他方式执行此操作吗?

此异常意味着您不能同时向
列表框添加带有
输入绑定的
列表框项目
,并绑定
项目资源

单击
ListBoxItem
时,有多种方法调用命令。其中之一是向
ItemTemplate
中的元素添加
InputBinding
,例如:

<ListBox ItemsSource="{Binding List}" Height="515" SelectedItem="{Binding SelectedItem}" Grid.Column="0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Background="Transparent">
                <Grid.InputBindings>
                    <MouseBinding MouseAction="LeftDoubleClick"
                                  Command="{Binding DataContext.Command, 
                                      RelativeSource={RelativeSource AncestorType=ListBox}}" />
                </Grid.InputBindings>
                <TextBlock Padding="4,1" Text="{Binding}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>


字符串上没有属性
命令
。。。另外,例外情况从何而来?你看过堆栈跟踪了吗?谢谢,这个解决方案有效。