I have a ListBox, each item has a TextBlock and a Button. The Button has a Command. The problem is, that the listbox's selecteditem doesn't change when I click the button. ( I guess the selectionchanged event doesn't fire). when I click the textblock, it works fine.
<ListBox ItemsSource="{Binding FavList}" SelectedItem="{Binding SelectedFav,Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Name}"/>
<Button Content="+" Command="{Binding Source={StaticResource ViewModel},Path=AddToFavCommand}" Margin="120,0,0,0"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Related parts of my viewmodel ( I don't think it has any problems ):
private Products _selectedFav;
public Products SelectedFav
{
get
{
return _selectedFav;
}
set
{
if (value != _selectedFav)
{
_selectedFav = value;
NotifyPropertyChanged("SelectedFav");
}
}
}
public DelegateCommand AddToFavCommand { get; set; }
AddToFavCommand = new DelegateCommand(addtofav);
private void addtofav(object parameter){
}
So I need to change the selected item before the command running. How can I do that?