mercredi 21 septembre 2016

Why changing ViewModel property does not update View checkbox eventhough RaisePropertyChanged is called?

I have a dialog/View and related "MainViewModel". "MainViewModel" has ObservableCollection of "RowViewModel" instances that represent a row with checkbox and textblock. Now the CheckBox binding works when I click checkbox (set, RaisePropertyChanged, get). But when I iterate all the RowViewModel-items in ObservableCollection and change bool value, only set and RaisePropertyChanged are called. get is not called and checkbox in view is not updated. What can be reason for this?

"RowViewModel" (ViewModel implements INotifyPropertyChanged and SetValue calls RaisePropertyChanged):

public class ViewObjectGroupViewModel : ViewModel
{
    public string Name { get; set; 

    private bool _isVisible;
    public bool IsVisible
    {
        get { return _isVisible; }
        set
        {
            SetValue(ref _isVisible, value))
        }
    }

"MainViewModel"

public class ViewViewModel : ViewModel
{
    private ItemsSource<ViewObjectGroupViewModel> _objectGroups;
    public ItemsSource<ViewObjectGroupViewModel> ObjectGroups
    {
        get { return _objectGroups; }
        set
        {
            SetValue(ref _objectGroups, value);
        }
    }

    // from command

                foreach (ViewObjectGroupViewModel objectGroup in ObjectGroups)
                {
                        objectGroup.IsVisible = newVisibleValue;
                }

XAML:

<ItemsControl ItemsSource="{Binding ObjectGroups}">
     <ItemsControl.ItemsPanel>
           <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
           </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     <ItemsControl.ItemTemplate>
           <DataTemplate>
                <StackPanel Orientation="Horizontal">
                     <CheckBox IsChecked="{Binding IsVisible, Mode=TwoWay}" IsThreeState="False"/>
                     <Label Content="{Binding VisibleName}" />
                </StackPanel>
           </DataTemplate>
     </ItemsControl.ItemTemplate>
</ItemsControl>




Aucun commentaire:

Enregistrer un commentaire