I know there are a lot questions about the SelectedItem and the ComboBox out there but it seems that they do not solve my problem.
I have a ComboBox where I bind a ObservableCollection of View Models and a TextBox which binds to the Description of the singe View models. I want to change the entries here... Everything works fine when i use the text box, the items will be changed in the ComboBox (even the selected one), but when i change the text in the code behind only the ComboBox List will be updated but not the current selected Value/Item.
My ComboBox looks like this at the moment:
<ComboBox ItemsSource="{Binding Sports, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding SelectedSport, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" Width="365" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Height="30" Margin="5 5 5 5">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The used ViewModel looks like this:
public class SportViewModel : ViewModelBase
{
private ObservableCollection<DisciplineViewModel> _disciplinesProperty;
public Sport Model
{
get;
set;
}
public string Description
{
get { return Model.Description; }
set
{
if (Model.Description != value)
{
Model.Description = value;
RaisePropertyChanged(() => Description);
}
}
}
public ObservableCollection<DisciplineViewModel> Disciplines
{
get { return _disciplinesProperty; }
set
{
if (_disciplinesProperty != value)
{
_disciplinesProperty = value;
RaisePropertyChanged(() => Disciplines);
}
}
}
public SportViewModel(Sport source)
{
Model = source;
Disciplines = new ObservableCollection<DisciplineViewModel>();
foreach(Discipline d in Model.Disciplines)
{
Disciplines.Add(new DisciplineViewModel(d, this));
}
}
public override bool Equals(object obj)
{
if (obj == null || !(obj is SportViewModel))
return false;
return ((SportViewModel)obj).Description == this.Description;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
and is used here:
class EditSportsDialogViewModel : ViewModelBase
{
...
public ObservableCollection<SportViewModel> Sports
{
get { return _sportsProperty; }
set
{
if (_sportsProperty != value)
{
_sportsProperty = value;
}
}
}
public SportViewModel SelectedSport
{
get { return _selectedSportPorperty; }
set
{
if (_selectedSportPorperty != value)
{
if (value != null)
{
Disciplines = value.Disciplines;
}
_selectedSportPorperty = value;
RaisePropertyChanged(() => SelectedSport);
}
}
}
private void SportRevertExecuted()
{
if (SelectedSport != null)
{
_context.Entry(SelectedSport.Model).Reload();
}
RaisePropertyChanged(() => SelectedSport.Description);
RaisePropertyChanged(() => SelectedSport);
RaisePropertyChanged(() => Sports);
RaisePropertyChanged();
}
}
The describtion is also changed by the one TextBox which works just fine
<TextBox Text="{Binding SelectedSport.Description, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" TextAlignment="Center" Margin="5" Width="410"/>
But when I call SportRevertExecuted() only the item in the list of the ComboBox will be changed but not the "Text" of the currently selected Item. Maybe someone can help me here, I've tried a lot until now, nothing seems to help.
Ok problem solved. Was quite stupid RaisePropertyChanged(() => SelectedSport.Description); doesn't work as i thought. I've introduced a Method UpdateView() in SportViewModel which raises the PropertyChanged Event in the right Object, now everything works just fine.
Related
I know this question may have been asked before but I can't find a solution for my problem and I will need some help.
What I want to accomplish :
I want to bind the exposed property Values of each item (ParameterValues) in my ListView to the ComboBox column and have the exposed property SelectedValue as selected item.
What I have done:
public class MyViewModel : MvvmTemplate //MvvmTemplate implements InotifyPropertyChanged
{
private ObservableCollection<ParameterValues> _parameterValuesTest;
public ObservableCollection<ParameterValues> ParameterValuesTest
{
get => _parameterValuesTest;
set
{
if (value == null)
return;
_parameterValuesTest = value;
RaisePropertyChanged();
}
}
public MyViewModel()
{
Parameters = new List<ParametersModel>(GetParametersFromDatabase());
ParameterValuesTest = new ObservableCollection<ParameterValues>();
ParameterValuesTest.Clear();
foreach(var param in parameters)
{
ParameterValuesTest.Add(new ParameterValues(param));
}
}
}
Parameters model :
public class ParametersModel:MvvmTemplate
{
public ParametersModel()
{
Parameters = new Parameters();
}
public ParametersModel(Parameters Parameters)
{
Parameters = Parameters;
}
public Parameters Parameters { get; set; }
public int Id
{
get => Parameters.Id;
set
{
if (Parameters.Id == value)
{
return;
}
Parameters.Id = value;
RaisePropertyChanged();
}
}
public string Value1
{
get => Parameters.Value1;
set
{
if (Parameters.Value1 == value)
{
return;
}
Parameters.Value1 = value;
RaisePropertyChanged();
}
}
public string Value2
{
get => Parameters.Value2;
set
{
if (Parameters.Value2 == value)
{
return;
}
Parameters.Value2 = value;
RaisePropertyChanged();
}
}
}
Parameters values model :
public class ParameterValues:MvvmTemplate
{
public ParameterValues(ParametersModel parameter)
{
ParametersModel = parameter;
Values = new ObservableCollection<string>
{
"Default Value",
ParametersModel.Value1,
ParametersModel.Value2
};
SelectedValue = Values.First();
}
public ParametersModel ParametersModel { get; set; }
private ObservableCollection<string> _values;
public ObservableCollection<string> Values
{
get => _values;
set
{
if (_values == value)
return;
_values = value;
RaisePropertyChanged();
}
}
private string _selectedValue;
public string SelectedValue
{
get => _selectedValue;
set
{
if (_selectedValue == value)
return;
_selectedValue = value;
RaisePropertyChanged();
}
}
}
XAML:
<ListView ItemsSource="{Binding Path=ParameterValuesTest}">
<ListView.View>
<GridView>
<GridViewColumn Width="Auto" Header="Id" DisplayMemberBinding="{Binding ParametersModel.Id}"/>
<GridViewColumn Header="Value">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<ItemsControl ItemsSource="{Binding Values, NotifyOnTargetUpdated=True}" DisplayMemberPath="SelectedValue" Height="0"/>
<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="SelectedValue" SelectedValuePath="SelectedValue" SelectedValue="{Binding SelectedValue}" TextSearch.TextPath="SelectedValue" Text="{Binding SelectedValue}" IsEditable="True"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Thanks in advance.
There are a few issues in your code.
In the parameters model, the assignment does not work, correct the parameter name.
public ParametersModel(Parameters parameters)
{
Parameters = parameters;
}
Remove the ItemsControl from the Grid, as it is hidden by the ComboBox or integrate it differently if you really need it, e.g. create rows or columns and place the controls in there.
The bindings in the ComboBox are wrong, you should bind to SelectedItem instead of using SelectedValue, since you have a collection of strings and not of a type that contains other properties that you want to bind to.
<ComboBox ItemsSource="{Binding Values}" SelectedItem="{Binding SelectedValue}" Text="{Binding SelectedValue}" IsEditable="True"/>
Remove the TextSearch attached property, as it is not used here.
I am having a problem with binding from a view to a Viewmodel property.(UWP)
<AppBarToggleButton Label="Active" Icon="People"
IsChecked="{x:Bind ViewModel.IsStatusBtnChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Click="{x:Bind ViewModel.empStautsBtnClicked}"/>
private bool isStatusBtnChecked = true;
public bool IsStatusBtnChecked
{
get { return isStatusBtnChecked; }
set { Set(ref isStatusBtnChecked, value); }
}
When I try to get the value from a method to load combobox items the value is allways the default value
private List<string> depcombo;
public List<string> Depcombo
{
get { return depcombo; }
set
{
if (depcombo != value)
{
depcombo = value;
OnPropertyChanged("Depcombo");
}
}
}
public async void GetDepCombo()
{
List<string> _dep = new List<string>();
var data2 = await SqlServerDataService.GetAllEmployeesAsync();
var depResult = (from emp in EmpItems
where emp.Status == IsStatusBtnChecked
select emp.Department).Distinct();
foreach (var item in depResult)
{
if (item != null)
{
_dep.Add(item);
}
}
Depcombo = _dep;
}
When I load the data for Employyes it works fine
public async Task LoadDataAsync(MasterDetailsViewState viewState)
{
EmpItems.Clear();
var data = await SqlServerDataService.GetAllEmployeesAsync();
data.Where(em => em.Status == IsStatusBtnChecked).ToList().ForEach(p => EmpItems.Add(p));
if (viewState == MasterDetailsViewState.Both)
{
Selected = EmpItems.FirstOrDefault();
}
}
Some help will be much appreciated
When I try to get the value from a method to load combobox items the value is allways the default value
It's is confused that which is the combobox ItemsSource, if Depcombo is ComboBox ItemsSource, You have passed a new list instance to ItemsSource when you call GetDepCombo method.
Depcombo = _dep;
So, we need to set bind mode as OneWay(OneTime is default) that could response the object instance change.
<ComboBox
Margin="0,120,0,0"
ItemsSource="{x:Bind MainViewModel.Depcombo, Mode=OneWay}"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
If EmpItems is ComboBox ItemsSource, and it is ObservableCollection type. When you call LoadDataAsync, EmpItems clear the items fist, then add the new items. And this processing does not change the EmpItems instance object. it could works in onetime mode.
<ComboBox Margin="0,120,0,0" ItemsSource="{x:Bind MainViewModel.EmpItems}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<AppBarToggleButton Label="Active" Icon="People"
x:Name="empStatusBtn"
IsChecked="{x:Bind ViewModel.IsStatusBtnChecked, Mode=TwoWay}"
Click="{x:Bind ViewModel.empStautsBtnClicked}"/>
public async void empStautsBtnClicked()
{
await LoadDataAsync(MasterDetailsViewState.Both);
}
Looks like the problem is when the view is reloaded. Is there a way to refresh the view without reloading. When reloading the value of
private bool isStatusBtnChecked = true;
public bool IsStatusBtnChecked
{
get { return isStatusBtnChecked; }
set
{
if (isStatusBtnChecked != value)
{
isStatusBtnChecked = value;
OnPropertyChanged("IsStatusBtnChecked");
}
}
}
is true but the button isChecked property is false;
I'm pretty new to WPF & MVVM, I'm having 3 CheckBox items on the panel, I need that each time when the user click one of them a method will be called. This method should check the status of each one of the 3 CheckBox (which one of them is 'Checked', and which of them is 'Unchecked') and use this information for displaying some text on a TextBlock. For example it will display "A" if all the 3 are Checked, and "B" if all of them are Unchecked.
I got little complicated with this, and hope that you can help me with this.
Here is the XAML code:
<StackPanel>
<ItemsControl Margin="5" ItemsSource="{Binding Path=CheckBoxCollection, Mode=TwoWay}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=CheckBoxValue, Mode=TwoWay}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock>Result Here</TextBlock>
</StackPanel>
Here is the code behind:
public class CheckBoxValue
{
bool _theValue;
public bool theValue
{
get
{
return _theValue;
}
set
{
_theValue = value;
}
}
}
public class myViewModel : ViewModelBase
{
public ObservableCollection<CheckBoxValue> CheckBoxCollection { get; set; }
public myViewModel( Some Parameters.... )
{
CheckBoxCollection = new ObservableCollection<CheckBoxValue>();
CheckBoxCollection.Add(new CheckBoxValue { theValue = false });
CheckBoxCollection.Add(new CheckBoxValue { theValue = false });
CheckBoxCollection.Add(new CheckBoxValue { theValue = false });
base.RaisePropertyChanged( () => CheckBoxCollection );
}
}
I'd go a step further and define a reusable ViewModel for all types of "Selectable" things:
public class Selectable<T>: ViewModelBase //ViewModelBase should Implement NotifyPropertyChanged.
{
private T _model;
public T Model
{ get { return _model; }
set
{
_model = value;
NotifyPropertyChange("Model");
}
}
public Action OnIsSelectedChanged;
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
NotifyPropertyChange("IsSelected");
if (OnIsSelectedChanged != null)
OnIsSelectedChanged();
}
}
}
See how I'm using a Delegate that will be called when IsSelected is changed, so that you can handle the selection change at the ViewModel level:
public class CheckBoxValue: Selectable<string>
{
//.. no need for any special stuff here
}
public class myViewModel : ViewModelBase
{
public ObservableCollection<CheckBoxValue> CheckBoxCollection { get; set; }
public myViewModel( Some Parameters.... )
{
CheckBoxCollection = new ObservableCollection<CheckBoxValue>();
CheckBoxCollection.Add(new CheckBoxValue { Model = "CheckBox1" });
CheckBoxCollection.Add(new CheckBoxValue { Model = "CheckBox2" });
CheckBoxCollection.Add(new CheckBoxValue { Model = "CheckBox3" });
//Setting IsSelected to false is redundant because bools default to false, so I removed that.
//Calling NotifyPropertyChange in the constructor is also redundant because the object is just being constructed, therefore WPF did not even read the initial values from it yet.
//Now, here we handle the Selection change:
foreach (var item in CheckBoxCollection)
item.OnIsSelectedChanged = OnCheckBoxSelectionChanged;
}
private void OnCheckBoxSelectionChanged()
{
//... etc
}
XAML:
<!-- Two way binding to the ItemsSource property is redundant, it doesn't make sense. -->
<ItemsControl ItemsSource="{Binding Path=CheckBoxCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- CheckBox.IsChecked BindsToWayByDefault, so it is also redundant
See http://msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadata.bindstwowaybydefault(v=vs.110).aspx -->
<!-- Also see how I'm adding content to the CheckBox here -->
<CheckBox Content="{Binding Model}"
IsChecked="{Binding Path=IsSelected}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
}
I probably changed this too much but this may be the simpler way of doing it.
<StackPanel>
<CheckBox IsChecked="{Binding Path=CheckBoxValue1, Mode=TwoWay}" />
<CheckBox IsChecked="{Binding Path=CheckBoxValue2, Mode=TwoWay}" />
<CheckBox IsChecked="{Binding Path=CheckBoxValue3, Mode=TwoWay}" />
<TextBlock>
<TextBlock.Text>
<Binding Path="StringValue" UpdateSourceTrigger="PropertyChanged" />
</TextBlock.Text>
</TextBlock>
</StackPanel>
#region properties
string stringValue;
public StringValue
{
get {return stringValue;}
set
{
stringValue = value;
OnPropertyChanged("StringValue");
}
}
bool checkBoxValue1;
public bool CheckBoxValue1
{
get{ return checkBoxValue1; }
set
{
checkBoxValue1= value;
ChangedValue();
}
}
bool checkBoxValue2;
public bool CheckBoxValue2
{
get{ return checkBoxValue2; }
set
{
checkBoxValue2 = value;
ChangedValue();
}
}
bool checkBoxValue3;
public bool CheckBoxValue3
{
get{ return checkBoxValue3; }
set
{
checkBoxValue3 = value;
ChangedValue();
}
}
#endregion
public class myViewModel : ViewModelBase
{
public myViewModel( Some Parameters.... )
{
checkBoxValue1 = false;
checkBoxValue2 = false;
checkBoxValue3 = false;
StringValue = b;
}
public void ChangedValue()
{
if (checkBoxValue1 && checkBoxValue2 && checkBoxValue3)
{
StringValue = a
}
else
{
StringValue = b
}
}
}
Sorry this is kind of thrown together but hopefully you get the idea
I'm new to MVVM, just recently started my first project following the MVVM pattern. I have an issue trying to validate an ObservableCollection using the IDataErrorInfo Interface. My ObservableCollection looks like this:
ObservableCollection<Magazine> magazineRepository;
public ObservableCollection<Magazine> MagazineRepository
{
get { return magazineRepository; }
set
{
if (value != null)
{
bladRepository = value;
OnPropertyChanged("MagazineRepository");
}
}
}
And my XAML like this:
<ListBox x:Name="listMagazineRepository"
Grid.ColumnSpan="2"
ItemsSource="{Binding}"
DataContext="{Binding MagazineRepository}"
DisplayMemberPath="Navn"
SelectedItem="{Binding Path=SelectedItem}"/>
<TextBox x:Name="txtName" Grid.Row="1" Grid.Column="0"
Text="{Binding ElementName=listMagazineRepository, Path=SelectedItem.Navn, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
<TextBox x:Name="txtPrice" Grid.Row="2" Grid.Column="0"
Text="{Binding ElementName=listMagazineRepository, Path=SelectedItem.Pris, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
It's just a simple listBox containing objects, when you select an item, the selected objects properties is displayed in the textboxes, and is then bound to the listbox object.
My Problem is, that when I set my code up like this, the only way I can figure out how to validate my data is in the Domain Model, which really isn't a good practise, I'd like to validate in the ViewModel before it gets there. Basically I want to validate each property in the MagazineRepository, in the ViewModel, How would you go about doing this?
PS: I'm new to posting on this board (and programming boards in general) if my question is lacking information, please let me know and I will supply the needed details.
Thanks a lot.
If i understand correctly you want to validate the Magazine object. If that's the case, one way to do it is to wrap that class in a viewmodel, let's call it MagazineVM, that implements IDataErrorInfo and keep the magazine object updated. You then bind to the view a list of MagazineVM. As a very simple example:
public class MagazineVM : IDataErrorInfo, INotifyPropertyChanged
{
private Magazine _magazine;
public int FirstMagazineProperty
{
get { return _magazine.FirstMagazineProperty; }
set { _magazine.FirstMagazineProperty = value; RaisePropertyChanged("FirstMagazineProperty"); }
}
//INotifyPropertyChanged implementation
//IDataErrorInfo implementation
}
Firstly, as Dtex says, you should use a MagazineViewModel class rather than a Magazine class. E.G.
public class MagazineViewModel : INotifyPropertyChanged, IDataErrorInfo
{
private string navn;
private string pris;
private string error;
public string Navn
{
get { return navn; }
set
{
if (navn != value)
{
navn = value;
RaisePropertyChanged("Navn");
}
}
}
public string Pris
{
get { return pris; }
set
{
if (pris != value)
{
pris = value;
RaisePropertyChanged("Pris");
}
}
}
public string Error
{
get { return error; }
set
{
if (error != value)
{
error = value;
RaisePropertyChanged("Error");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public string this[string columnName]
{
get
{
var result = string.Empty;
switch (columnName)
{
case "Pris":
if (string.IsNullOrWhiteSpace(Pris))
{
result = "Pris is required";
}
break;
case "Navn":
if (string.IsNullOrWhiteSpace(Navn))
{
result = "Navn is required";
}
break;
}
return result;
}
}
private void RaisePropertyChanged(string PropertyName)
{
var e = PropertyChanged;
if (e != null)
{
e(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
The important property to note is "public string this[string columnName]". ColumnName will be one of your bound properties and this is where you can do validation.
The next thing to consider is your MainViewModel (Your DataContext). E.G.
public class MainViewModel : INotifyPropertyChanged
{
//Use a readonly observable collection. If you need to reset it use the .Clear() method
private readonly ObservableCollection<MagazineViewModel> magazines = new ObservableCollection<MagazineViewModel>();
private MagazineViewModel selectedItem;
//Keep the item being edited separate to the selected item
private MagazineViewModel itemToEdit;
public ObservableCollection<MagazineViewModel> Magazines { get { return magazines; } }
public MagazineViewModel SelectedItem
{
get { return selectedItem; }
set
{
if (selectedItem != value)
{
selectedItem = value;
RaisePropertyChanged("SelectedItem");
//When the selected item changes. Copy it to the ItemToEdit
//This keeps the the copy you are editing separate meaning that invalid data isn't committed back to your original view model
//You will have to copy the changes back to your original view model at some stage)
ItemToEdit = Copy(SelectedItem);
}
}
}
public MagazineViewModel ItemToEdit
{
get { return itemToEdit; }
set
{
if (itemToEdit != value)
{
itemToEdit = value;
RaisePropertyChanged("ItemToEdit");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public MainViewModel()
{
//Ctor...
}
//Create a copy of a MagazineViewModel
private MagazineViewModel Copy(MagazineViewModel ToCopy)
{
var vm = new MagazineViewModel();
vm.Navn = ToCopy.Navn;
vm.Pris = ToCopy.Pris;
return vm;
}
private void RaisePropertyChanged(string PropertyName)
{
//...
}
}
The only thing missing here is how you copy the changes back to the original view model. You could do it before the selected item changes (if the ItemToEdit is valid) or have a Commit button that is only enabled when the ItemToEdit is valid. If you can allow your original view models to go into an invalid state you don't need to worry about the copying.
Finally the XAML
An implicit style to show the error tooltip
<Style
TargetType="{x:Type TextBox}">
<Setter
Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Style>
And the controls and bindings
<ListBox
ItemsSource="{Binding Magazines}"
DisplayMemberPath="Navn"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" />
<TextBox
Margin="5"
x:Name="txtName"
Grid.Row="1"
Grid.Column="0"
Text="{Binding ItemToEdit.Navn, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
<TextBox
Margin="5"
x:Name="txtPrice"
Grid.Row="2"
Grid.Column="0"
Text="{Binding ItemToEdit.Pris, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
The TextBoxes bind to ItemToEdit. ItemToEdit will be an in-sync copy of the SelectedItem.
I have the following ui items - one checkbox list and one checkbox with toggle all checkboxes in that list -
<DockPanel>
<CheckBox
Name="SelectCheckboxes"
Command="{Binding ToggleCheckBoxes}"
Content="Whatever"/>
</DockPanel>
<DockPanel>
<ListBox Name="MyListBox"
ItemsSource="{Binding Path=MyProperty, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="MyCheckBox"
Content="{Binding myvalue}"
Tag="{Binding mycode}"
IsChecked="{Binding Path=isChecked, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
And here is the MyProperty property -
public ObservableCollection<SomeEntity> MyProperty
{
get { return _someEntities; }
set
{
if (value == _someEntities)
return;
_someEntities = value;
base.OnPropertyChanged("MyProperty");
}
}
And here is a command ToggleCheckBoxes -
public ICommand ToggleCheckBoxes
{
get
{
if (_toggleCheckBoxesCommand == null)
{
_toggleCheckBoxesCommand = new RelayCommand(
param => this.toggleCheckBoxes()
);
}
return _toggleCheckBoxesCommand;
}
}
void toggleCheckBoxes()
{
foreach (var i in MyProperty)
{
if (i.isChecked)
i.isChecked = false;
else
i.isChecked = true;
}
}
When I click on the checkbox to toggle the checkboxes, I can look at the property in the code and see that the isChecked property is changed, but the ListBox does not update to reflect that all items are checked/unchecked.
Does anyone see anything that I am missing that might cause the ListBox not to update?
Thnaks for any thoughts.
Make sure that your isChecked member is actually a property and that SomeEntity implements INotifyPropertyChanged. Something like:
public class SomeEntity : INotifyPropertyChanged {
private bool _isChecked;
public bool isChecked
{
get { return _isChecked; }
set
{
if (value == _isChecked)
return;
_isChecked= value;
this.NotifyPropertyChanged("isChecked");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}