Hilto
Hilto

Reputation: 21

Why can I not bind a command to the double click of a listbox command

I am trying to bind a list of numbers to a listbox and set a doubleClickCommand on so that when I double click on an Item it will run the SetItem Method.

My View

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

And my 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); }
    }
}

When I try to run the code I get this exception.

InvalidOperationException: Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead

Is there a way to fix this; or do I need to do this some other way?

Upvotes: 0

Views: 61

Answers (1)

mm8
mm8

Reputation: 169240

The exception means that you cannot both add a ListBoxItem with an InputBinding to the ListBox and bind the ItemsSource at the same time.

There are multiple ways to invoke a command when a ListBoxItem is clicked. One of them is to add an InputBinding to an element in the ItemTemplate, e.g.:

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

Upvotes: 2

Related Questions