Reputation: 3
WPF MVVM Listbox of Checkboxes IsChecked not firing on ViewModel when Checked The List Box is bound to the ObservableCollection and the Text comes out fine and populates listbox correctly. When it is checked nothing happens. I have tried binding it to a boolean value in the ObservableCollection and a dependency on the view model but neither fire. Any Help would be great.
Update to Question: I am now getting the IsChecked to fire but now I need to grab the content(text(Value in this bind->"Store")) of the checkbox when IsChecked fires. I would settle for the Index of the checkbox that was checked. I have posted code below. Here is my XAML:
<ListBox ItemsSource="{Binding Stores}" SelectedItem="{Binding SelectedItem}" HorizontalAlignment="Center" VerticalAlignment="Top" Width="200" Height="400" SelectionMode="Multiple" IsEnabled="True">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Content="{Binding Store}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Here is My MainViewModel:
private IsCheckedViewModel _selectedItem;
public EbayModel SelectedItem
{
get { return _selectedItem; }
set
{
if(_selectedItem!=value)
{
_selectedItem = value;
//Need to Grab Content Value when Checkbox is Checked.
//This works when you click on the right side of check box
//but I need the value when IsChecked fires.
OnPropertyChanged("SelectedItem");
}
}
}
private ObservableCollection<IsCheckedViewModel> _stores;
public ObservableCollection<IsCheckedViewModel> Stores
{
get { return _stores; }
set
{
if(_stores!=value)
{
_stores = value;
OnPropertyChanged("Stores");
}
}
}
Here is the IsCheckedViewModel(IsChecked works and fires back now)
private string _store;
public string Store
{
get { return _store; }
set
{
if (_store != value)
{
_store = value;
OnPropertyChanged("Store");
}
}
}
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
if (_isChecked != value)
{
//This works
_isChecked = value;
OnPropertyChanged("IsChecked");
}
}
}
Upvotes: 0
Views: 2272
Reputation: 12857
You should have a ViewModel class inside your observable collection, that viewmodel should have a bool IsChecked
property in it. This view model class should implement the INotifyPropertyChanged interface like all view models.
Your XAML that binds the ObservableCollection<> should Bind the ItemSource and it should bind the CurrentItem to a dedicated property to the VM.
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<ItemViewModel> _list = new ObservableCollection<ItemViewModel>();
//These setters should trigger the PropertyChanged event
public ObservableCollection<ItemViewModel> Items{
get {...}
}
public ItemViewModel SelectedItem {
get {...}
set {...}
}
}
//Now you need another view model
public class ItemViewModel: INotifyPropertyChanged {
private bool _IsChecked;
public bool IsChecked{
get {...}
set { ... }
}
XAML:
ItemsSource="{Binding Items}"
CurrentItem="{Binding SelectedItem}"
Then on the XAML of the checkbox the DataContext will be of type ItemViewModel so set the IsChecked binding to the IsChecked property to the ItemViewModel class:
IsChecked="{Binding IsChecked}"
Upvotes: 0
Reputation: 3
After working with this I got my answer: Here is the xaml:
<ListBox ItemsSource="{Binding Stores}" SelectedItem="{Binding SelectedItem}" HorizontalAlignment="Center" VerticalAlignment="Top" Width="200" Height="400" SelectionMode="Multiple" IsEnabled="True">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsChecked,Mode=OneWay}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Content="{Binding Store}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Here is my MainViewModel:
private IsCheckedViewModel _selectedItem;
public IsCheckedViewModel SelectedItem
{
get { return _selectedItem; }
set
{
if(_selectedItem!=value)
{
_selectedItem = value;
//Here I Get My Text Value.
string Astore = SelectedItem.Store;
OnPropertyChanged("SelectedItem");
}
}
}
private ObservableCollection<ISCheckedModel> _stores;
public ObservableCollection<IsCheckViewModel> Stores
{
get { return _stores; }
set
{
if(_stores!=value)
{
_stores = value;
OnPropertyChanged("Stores");
}
}
}
Here is my IsCheckedViewModel:
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
if (_isChecked != value)
{
_isChecked = value;
OnPropertyChanged("IsChecked");
}
}
}
Thank you T McKeown for your answer!! It got me going down the right path!!!
Upvotes: 0