locked
MVVM: ListView not responding to SelectedItem changing RRS feed

  • Question

  • Hi folks,

    I have a ListView with SelectionMode="Single". Each items contain a checkbox. When this checkbox is ticked, I want the ListView to select the item that is the parent of the ticked/unticked checkbox.

    The ListView looks like this:

    <ListView 
    	ItemsSource="{Binding Path=MyItemsCollection}"
    	SelectedItem="{Binding Path=MySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    	IsSynchronizedWithCurrentItem="True"
    	SelectionMode="Single"
    	Width="Auto"
    	Margin="5,3,5,5" 
    	>
    	<ListView.View>
    		<GridView AllowsColumnReorder="False" >
    			<GridViewColumn Header="SomeCol" DisplayMemberBinding="{Binding Path=SomeProperty}" Width="Auto"/>
    			<GridViewColumn Width="Auto" Header="CheckboxCol">
    				<GridViewColumn.CellTemplate>
    					<DataTemplate>
    						<CheckBox HorizontalContentAlignment="Center" HorizontalAlignment="Center"
    							 IsChecked="{Binding Path=MyBoolProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    							 Command="{Binding Path=DataContext.MyCheckboxChangedCommand, 
    									RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
    							 CommandParameter="{Binding Path=MyCommandParam}"
    						 />
    					</DataTemplate>
    				</GridViewColumn.CellTemplate>
    			</GridViewColumn>
    		</GridView>
    	</ListView.View>
    </ListView>

    Since I'm following the MVVM-pattern, ticking the Checkbox results in some code in the ViewModel being executed (the MyCheckboxChangedCommand in the sample code).
    There, the MySelectedItem property will be changed to point to the item that is behind the checkbox that was clicked. The MySelectedItem-property also implements INotifyPropertyChanged; this is working as expected - I can confirm the property change being applied due to the fact that there's TextBlock in my window that correctly changes its Text-property to show the text associated with MySelectedItem.
    However, the ListView's SelectedItem is not being updated - it is not selecting/highlighting the row when MySelectedItem is being changed by the ViewModel.

    Am I missing something obvious here ..?

    Cheers,
    Olaf
    Friday, October 9, 2009 4:34 PM

Answers

  • My guess, from what you're describing, is that your not setting MySelectedItem to the appropriate item in the collection.  It's difficult to tell completely, since your only confirmation is a text box that's not shown in the code you pasted...
    What is "MyCommandParam" in this case? 

    You could just set your command parameter to the current ListView item via CommandParameter="{Binding}" , and then, in your command, just set "MySelectedItem" directly to the parameter passed to the command.  This would simplify things a bit, probably.



    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, October 9, 2009 5:01 PM

All replies

  • My guess, from what you're describing, is that your not setting MySelectedItem to the appropriate item in the collection.  It's difficult to tell completely, since your only confirmation is a text box that's not shown in the code you pasted...
    What is "MyCommandParam" in this case? 

    You could just set your command parameter to the current ListView item via CommandParameter="{Binding}" , and then, in your command, just set "MySelectedItem" directly to the parameter passed to the command.  This would simplify things a bit, probably.



    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, October 9, 2009 5:01 PM
  • Hi Reed,

    gee, I must've been blind or something - I spent over an hour on this, obviously not seeing the obvious.

    You were right on track - the CommandParameter was there just because I wasn't able to pass the SelectedItem to my ViewModel when the command was executed, hence I passed a parameter that allowed the ViewModel to identify the record by other means. Now that I replaced this with your CommandParameter="{Binding}" and changed the ViewModel accordingly, it's working.

    Thanks!

    Cheers,
    Olaf
    Friday, October 9, 2009 5:10 PM