Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

PropertyChanged Null when not visible

$
0
0

hi,

I am having issue with a Collection shown in a Listview giving null on PropertyChanged when the item is not visible in the UI

Here is what i am trying to do.

I have a Checkbox in the list view that is registered to Checked

<ListView Name="lvSuites" 
                  Grid.Row="3" Grid.ColumnSpan="2"
                  Margin="10,10,10,10"
                  ItemsSource="{Binding Path=SuiteList, Mode=TwoWay}"
                  Background="{StaticResource PressedBrush}"
                  BorderBrush="{StaticResource SolidBorderBrush}" Foreground="Black"><ListView.View><GridView><GridViewColumn Width="Auto"><GridViewColumn.CellTemplate><DataTemplate><CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Tag="{Binding Path=Name, Mode=TwoWay}" Checked="CheckBoxChecked" Unchecked="CheckBoxUnchecked"/></DataTemplate></GridViewColumn.CellTemplate></GridViewColumn><GridViewColumn Width="40" Header="#" ><GridViewColumn.CellTemplate><DataTemplate><TextBlock TextAlignment="Center" Text="{Binding Path=TestNumber, Mode=TwoWay}"/></DataTemplate></GridViewColumn.CellTemplate></GridViewColumn><GridViewColumn Width="300" Header="Suite"><GridViewColumn.CellTemplate><DataTemplate><TextBlock TextAlignment="Left" Text="{Binding Path=Name, Mode=TwoWay}"/></DataTemplate></GridViewColumn.CellTemplate></GridViewColumn></GridView></ListView.View></ListView>

When i call

private void ListSuiteAllMouseUp(object sender, MouseButtonEventArgs e)
        {
            foreach (var suite in ((ViewModel)DataContext).SuiteList)
            {
                suite.IsChecked = true;
            }
        }

PropertyChanged is null for the items that are not visible in the Scrollview of the Listview, but the ones that are visible call Checked of the checkbox

    public class Suite : INotifyPropertyChanged
    {
        private bool ischecked;
        private string name;
        private int testNumber;

        public Suite()
        {
            IsChecked = false;
        }

        public bool IsChecked
        {
            get { return ischecked; }
            set
            {
                ischecked = value;
                OnPropertyChanged("IsChecked");
            }
        }

        public int TestNumber
        {
            get { return testNumber; }
            set
            {
                testNumber = value;
                OnPropertyChanged("TestNumber");
            }
        }

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

and i have my ViewModel 

DataContext = ViewModel();

private void CheckBoxChecked(object sender, RoutedEventArgs e)
        {
            //Do Something
        }


Can anyone help me get the PropertyChanged to not be null even for items that arnt visible in the ListView bc they are farther down the ScrollView becuase i want Checked to be called if i changed IsChecked property or if the user actually checks it. if i scroll down the list view and try setting IsChecked= true it works for the items that are now visible. It seems like PropertyChanged doesnt get attached till the object is rendered.



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>