locked
wpf observablecollection change item RRS feed

  • Question

  • Hello everyone,
    I need an advice. I have a ListBox where I put the items with a movie title.
    How can I make when you double-click on an item in the listbox that you check the checkbox. But when I click on another item I need to make all other Checkbox were ticked off. Thanks to the advice

    I attach source code WpfApplication 

    I think it has something to do with observablecollection and power are not familiar.

      foreach (object o in listBox.SelectedItems)
                {
                    string items = (o as MyModel).NameMovie;
    
                    var movie = List.FirstOrDefault(m => m.NameMovie == items);
                    if (movie != null)
                    {
                        
                        movie.IscheckedVideo = true;
                    }
                    else
                    {
                        
                        movie.IscheckedVideo = false;
                    }
                }


     public class MyModel : INotifyPropertyChanged
        {
            private string _nameMovie;
            private bool _isCheckedVideo;
         
    
    
            public string NameMovie
            {
                get { return _nameMovie; }
                set { _nameMovie = value; NotifyPropertyChanged("NameMovie"); }
            }
    
    
            public bool IscheckedVideo
            {
                get { return _isCheckedVideo; }
                set { _isCheckedVideo = value; NotifyPropertyChanged("IscheckedVideo"); }
    
            }
    
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(string property)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                   
                }
            }
        }
     <DataTemplate x:Key="EmployeeDataTemplate">
                <Grid d:DesignWidth="477.775" d:DesignHeight="53.079" Height="61">
                    <TextBlock x:Name="textBlock" HorizontalAlignment="Stretch" Margin="53,14,18.775,0" TextWrapping="Wrap" Text="{Binding NameMovie}" VerticalAlignment="Top" FontSize="20" Foreground="#FFFF073F" Height="27"/>
                    <CheckBox x:Name="checkBox" IsChecked="{Binding IscheckedVideo}" Content="" HorizontalAlignment="Left" Margin="14,21,0,0" VerticalAlignment="Top" Width="16"/>
                </Grid>
            </DataTemplate>



    Tuesday, March 1, 2016 7:31 PM

Answers

  • You could hook up an event handler for the PropertyChanged event of the MyModel object before you add it to the collection in your btnOpen method:

    private void btnOpen(object sender, RoutedEventArgs e)
            {
                System.Windows.Forms.OpenFileDialog OpenFileDialog1 = new System.Windows.Forms.OpenFileDialog();
                OpenFileDialog1.Multiselect = true;
                if (OpenFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
                {
    
                    OpenFileDialog1.Multiselect = true;
                    foreach (string filename in OpenFileDialog1.SafeFileNames)
                    {
                        MyModel model = new MyModel { NameMovie = filename, IsPlayingVideo = false };
                        model.PropertyChanged += Model_PropertyChanged;
                        List.Add(model);
                    }
                }
            }
    
            private bool _handle = true;
            private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                if (e.PropertyName == "IsPlayingVideo" && _handle)
                {
                    _handle = false;
                    foreach (MyModel myModel in _list.Where(m => m != sender))
                        myModel.IsPlayingVideo = false;
                    _handle = true;
                }
            }
    

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by ORRNY66 Tuesday, March 1, 2016 9:14 PM
    Tuesday, March 1, 2016 8:52 PM

All replies

  • I would think you need to use a RadioButton.  A RadioButton has a GroupName which means that only one RadioButton within the Group can be checked at any time.  Therefore when you "check" an item all other items within the Group should have their IsChecked set to false.


    Lloyd Sheen

    Tuesday, March 1, 2016 8:19 PM
  • I would just set the selection mode to single and bind the checkbox ischecked to listboxitem isselected.

    Binding listboxitem isselected to

            public bool IscheckedVideo
            {
                get { return _isCheckedVideo; }
                set { _isCheckedVideo = value; NotifyPropertyChanged("IscheckedVideo"); }
    
            }

    You can only select one entry then.

    Maybe that obviates your checkbox entirely.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, March 1, 2016 8:31 PM
  • You could hook up an event handler for the PropertyChanged event of the MyModel object before you add it to the collection in your btnOpen method:

    private void btnOpen(object sender, RoutedEventArgs e)
            {
                System.Windows.Forms.OpenFileDialog OpenFileDialog1 = new System.Windows.Forms.OpenFileDialog();
                OpenFileDialog1.Multiselect = true;
                if (OpenFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
                {
    
                    OpenFileDialog1.Multiselect = true;
                    foreach (string filename in OpenFileDialog1.SafeFileNames)
                    {
                        MyModel model = new MyModel { NameMovie = filename, IsPlayingVideo = false };
                        model.PropertyChanged += Model_PropertyChanged;
                        List.Add(model);
                    }
                }
            }
    
            private bool _handle = true;
            private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                if (e.PropertyName == "IsPlayingVideo" && _handle)
                {
                    _handle = false;
                    foreach (MyModel myModel in _list.Where(m => m != sender))
                        myModel.IsPlayingVideo = false;
                    _handle = true;
                }
            }
    

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by ORRNY66 Tuesday, March 1, 2016 9:14 PM
    Tuesday, March 1, 2016 8:52 PM
  • Thanks very much you helped me.
    Tuesday, March 1, 2016 9:14 PM