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

Items in listboxes and comboboxes are not updated if the bound list changes

$
0
0

Issue is solved: Use ObservableCollection<string> instead of List<string>!

The sample project shows the list elements. There are two commands to add or delete list items. The list is updated, even the item count in label of the combobox. 

But the drop down section in the combobox and the list section in the listbox are not updated.

XAML of the window with Ribbon, ComboBoxes and ListBox.

                        

<Window x:Class="TestRibbonCombo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" >
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="200"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Ribbon Margin="5
                ">
            <RibbonGroup Header="combo only" Width="120">
                <RibbonComboBox Label="{Binding RibbonList.Count}" ItemsSource="{Binding RibbonList}" />


            </RibbonGroup>
            <RibbonGroup Header="combo and gallery" Width="120">
                <RibbonComboBox Label="{Binding RibbonList.Count}" >
                    <RibbonGallery  SelectedItem="{Binding SelectedItem}"   ItemsSource="{Binding RibbonList}" />                   
                </RibbonComboBox>
            </RibbonGroup>

            <RibbonGroup Header="Select item" Width="150">
                <RibbonComboBox Label="{Binding RibbonList.Count}" >
                    <RibbonGallery  SelectedItem="{Binding SelectedItem}"   >
                        <RibbonGalleryCategory Header="-- item list --"
                                               ItemsSource="{Binding RibbonList}"                                              
                                                         />
                    </RibbonGallery>
                </RibbonComboBox>
            </RibbonGroup>
            <RibbonGroup Header="Commands">

                <RibbonButton Width ="70" Height="40" Label="Add item" Background="LightGreen" Command="{Binding AddItem}" Margin="5"></RibbonButton>
                <RibbonButton Width ="70" Height="40" Label="Delete item" Background="LightSalmon" Command="{Binding DeleteItem}" Margin="5"></RibbonButton>
            </RibbonGroup>

        </Ribbon>
        <TextBlock Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" Text="Elements in listbox or comboboxes are not updated if item list changes." Background="Beige"></TextBlock>

        <StackPanel Grid.Row="2" Orientation="Horizontal" Background="Azure" Margin="5">
            <ListBox ItemsSource="{Binding RibbonList}" Width="200" VerticalAlignment="Stretch" Margin="10"></ListBox>
            <TextBlock  Text="{Binding SelectedItem}" Background="Azure" Margin="10"></TextBlock>
            <TextBlock  Text="{Binding RibbonListStrings}" Background="Beige" Margin="10"></TextBlock>
        </StackPanel>
    </Grid>
</Window>

constructor of MainWindow

  public p

artial class MainWindow : Window
    {
        public MainWindow()
        {
            ViewModel vm = new ViewModel();
            InitializeComponent();
            vm.CreateCommands(this);
            DataContext = vm;
        }
    }

View model

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace TestRibbonCombo
{
    public class ViewModel : ViewModelBase
    {
        public ViewModel()
        {
            RibbonList = new List<string>();
            RibbonList.Add("Item 1");
            RibbonList.Add("Item 2");
            RibbonList.Add("Item 3");
            RibbonList.Add("Item 4");
        }

        List<string> _RibbonList;
        public List<string> RibbonList
        {
            get { return _RibbonList; }
            set 
            {
                if(_RibbonList != value)
                {
                    _RibbonList = value;
                    NotifyPropertyChanged(GetPropertyName(() => RibbonList));
                }
            }
        }


        public string RibbonListStrings
        {
            get 
            {
                string items = _RibbonList.Count.ToString() + " items: ";
                foreach (var item in _RibbonList) { items += item + "; "; }
                return items; 
            }
        }

        string _SelectedItem;
        public string SelectedItem
        {
            get { return _SelectedItem; }
            set
            {
                if (_SelectedItem != value)
                {
                    _SelectedItem = value;
                    NotifyPropertyChanged(GetPropertyName(() => SelectedItem));
                }
            }
        }


        bool canAdd()
        {
            return true;
        }
        bool canDelete()
        {
            return SelectedItem != null;
        }


        void execAdd()
        {
            RibbonList.Add("Item " + DateTime.Now.ToLongTimeString());
            NotifyPropertyChanged(GetPropertyName(() => RibbonList));
            NotifyPropertyChanged(GetPropertyName(() => RibbonListStrings));

        }
        void execDelete()
        {
            RibbonList.Remove(SelectedItem);
            SelectedItem = null;
            NotifyPropertyChanged(GetPropertyName(() => RibbonList));
            NotifyPropertyChanged(GetPropertyName(() => RibbonListStrings));
        }
        public ICommand AddItem { get; set; }
        public ICommand DeleteItem { get; set; }

        public override void CreateCommands(System.Windows.FrameworkElement fw)
        {
            AddItem = new RoutedCommand("Add Item", fw.GetType());
            AddCommand(AddItem, fw);
            DeleteItem = new RoutedCommand("Delete Item", fw.GetType());
            AddCommand(DeleteItem, fw);
        }
        public override void CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            if (e.Command == AddItem) { e.CanExecute = canAdd(); return;}
            else if (e.Command == DeleteItem) { e.CanExecute = canDelete(); return; }
        }
        public override void Executed(object o, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            if (e.Command == AddItem) {execAdd(); return; }
            else if (e.Command == DeleteItem) { execDelete(); return; }
        }
    }
    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public static string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
        {
            var me = propertyLambda.Body as MemberExpression;
            if (me == null)
            {
                throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
            }
            return me.Member.Name;
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
        #region command framework

        /// <summary>
        /// method to be implemented
        /// </summary>
        /// <param name="fw"></param>
        public abstract void CanExecute(object sender, CanExecuteRoutedEventArgs e);
        public abstract void Executed(object o, ExecutedRoutedEventArgs e);



        public abstract void CreateCommands(FrameworkElement fw);
        // public abstract void CreateCommands();
        public void AddCommand(ICommand command, FrameworkElement fw)
        {
            CommandBinding cb = new CommandBinding();
            cb.Command = command;
            cb.CanExecute += CanExecute;
            cb.Executed += Executed;
            fw.CommandBindings.Add(cb);
        }

        #endregion command framework

    }
}




Viewing all articles
Browse latest Browse all 18858

Trending Articles



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