none
ListViewItem and Command handling?

    Question

  • Hello, I have defined a window under MVVM approach. Everything works great, but I found a little problem.

    I have a ListView with items from a collection, all that I want is to track the SelectedItem changes to a command.

     

      <Grid>
        <Grid>
          <Grid>
            <Label>Player</Label>
            <Label Grid.Row="1">Points</Label>
            <TextBox Grid.Column="1" Text="{Binding CurrentPlayer.Name}"></TextBox>
            <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding CurrentPlayer.Points}"></TextBox>
          </Grid>
          <WrapPanel Grid.Row="1">
            <Button Margin="10" Padding="6,0" Command="{Binding NewPlayerCommand}">Nuevo</Button>
            <Button Margin="10" Padding="6,0" Command="{Binding SavePlayerCommand}">Grabar</Button>
            <Button Margin="10" Padding="6,0" Command="{Binding DeletePlayerCommand}">Eliminar</Button>
          </WrapPanel>
        </Grid>
        <ListView Grid.Column="1" ItemsSource="{Binding PlayerCollection}">
        </ListView>
      </Grid>
    
    

    How do I attach a "current selected item" command to my ListView?

    I mean, how do I create a command to notify my ModelView about any ListView (items) selection changes.

    I know code behind is not bad, but there must be a code-less way. Or not?

     

    Thank you in advance

     

     

    Monday, September 20, 2010 2:37 PM

Answers

  • Hi there,

    create a property in your VM (i.e. TheSelectedItem ) and bind your ListView's SelectedItem to it. You'll need to set IsSynchronizedWithCurrentItem="True" on your LV, too. As for your VM's TheSelectedItem -property, you should implement INotifyPropertyChanged for the property so as to have your View be notified when you set the property from within your VM. All this doesn't require any code-behind in the View.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by zameb Monday, September 20, 2010 3:33 PM
    Monday, September 20, 2010 3:13 PM

All replies

  • Hi there,

    create a property in your VM (i.e. TheSelectedItem ) and bind your ListView's SelectedItem to it. You'll need to set IsSynchronizedWithCurrentItem="True" on your LV, too. As for your VM's TheSelectedItem -property, you should implement INotifyPropertyChanged for the property so as to have your View be notified when you set the property from within your VM. All this doesn't require any code-behind in the View.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by zameb Monday, September 20, 2010 3:33 PM
    Monday, September 20, 2010 3:13 PM
  • Woah Olaf, it was a very straightforward help.

    I already had a DP called CurrentPlayer, so I could to bind it to the SelectedItem and everything worked very well.

    Here it is the code for others that need the same.

    Thank you!

      <Grid>
        <Grid>
          <Grid>
            <Label>Player</Label>
            <Label>Points</Label>
            <TextBox Text="{Binding CurrentPlayer.Name}"></TextBox>
            <TextBox Text="{Binding CurrentPlayer.Points}"></TextBox>
          </Grid>
          <WrapPanel>
            <Command="{Binding NewPlayerCommand}">New</Button>
            <Button Command="{Binding SavePlayerCommand}">Save</Button>
            <Button Command="{Binding DeletePlayerCommand}">Delete</Button>
          </WrapPanel>
        </Grid>
        <ListView ItemsSource="{Binding PlayerCollection}" 
             SelectedItem="{Binding CurrentPlayer}" IsSynchronizedWithCurrentItem="True">
        </ListView>
      </Grid>
    
    

    Monday, September 20, 2010 3:38 PM
  • My problem was really solved, but I can't avoid to think in a related and more generic problem:

    The Button control has a Command property, which can be assigned to a custom command, but a ListViewItem (and other UI elements) doesnt. How could you assign a command over a ListViewItem action?

    In other words, is worth to use a Command approach to answer my original question?

    Thank you

    Monday, September 20, 2010 3:55 PM
  • Hi there,

    well, usually, you'd react to your selection-change (IOW, your "command") in what I called the TheSelectedItem -property. If you need to disable an item, you'd introduce yet another property which would indicate just that.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Monday, September 20, 2010 4:15 PM
  • Hi, I have learned a lot... and still learning.

    There is always a context menu that I could use in any control. This context menu allows the user to select one among many actions on TheSelectedItem that Olaf suggest. And the better is that each menu item can be bound to a command.

    Greetings

    Monday, October 25, 2010 9:13 AM