Ask a questionAsk a question
 

Answerwpf - mvvm - listbox - IsEnabled

  • Saturday, November 07, 2009 11:22 AMYehuda A Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi
    I have a listbox which I want to set its property IsEnabled while clicking on button. 
    I tried to bind the button command  to a method on the ViewModel which supposed to changh a boolean parameter to TRUE \ FALSE, and the listbox is bind to a property which get and set this parameter. it doesnt work.

    here is my code:
    in xaml:
    <Button Canvas.Left="743.213" Canvas.Top="58" Height="114" Width="113.625" Template="{DynamicResource ButtonControlTemplate2}" Command="{Binding NewListRecord}" >make list </Button> 

    <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" FontFamily="Ariel" Foreground="OrangeRed" Margin="0.5,0,1,0" Grid.Column="1" IsEnabled="{Binding isProductListEnabled}"></ListBox>

    ViewModel:

    private

     

    bool productListEnabledDisabled;

     

    public ICommand NewListRecord
    {
          get
          {
               if (newListRecord == null)
              {
                      newListRecord =
    new DelegateCommand(RecordNewList);
              }
              return newListRecord;
         }
    }

     

    private void RecordNewList()
    {
         this.isProductListEnabled = true;
    }

    public

     

    bool isProductListEnabled
    {
           get { return productListEnabledDisabled; }
           set { this.productListEnabledDisabled = value;}
    }



    can anyone help me with this?

Answers

  • Saturday, November 07, 2009 11:53 AMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Yehuda,

    to make this work, you'll have to notify the View that the isProductListEnabled property has changed. To do so, implement INotifyPropertyChanged in your ViewModel. Here's a sample that shows how to work this out; I didn't follow MVVM here for the sake of simplicity:

    <Window x:Class="WpfTests.ListBoxIsEnabled"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="ListBoxIsEnabled" Height="300" Width="300"
       DataContext="{Binding RelativeSource={RelativeSource Self}}"
       >
    
       <Grid>
          <Grid.RowDefinitions>
             <RowDefinition Height="Auto"/>
             <RowDefinition Height="*"/>
             <RowDefinition Height="Auto"/>
          </Grid.RowDefinitions>
          <Button Grid.Row="0" Content="Toggle ListBox'es IsEnabled-prop" Click="Button_Click" Margin="5" Padding="5"/>
          <ListBox Grid.Row="1" Foreground="OrangeRed" IsEnabled="{Binding IsListBoxEnabled}" Margin="5" SelectedIndex="0">
             <ListBoxItem Content="Item1"/>
             <ListBoxItem Content="Item2"/>
          </ListBox>
          <CheckBox Grid.Row="2" Content="ListBox is enabled" IsChecked="{Binding IsListBoxEnabled}" Margin="5"/>
       </Grid>
    </Window>
    
    

    And the code-behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.ComponentModel;
    
    namespace WpfTests
    {
       /// <summary>
       /// Interaction logic for ListBoxIsEnabled.xaml
       /// </summary>
       public partial class ListBoxIsEnabled : Window, INotifyPropertyChanged
       {
    
          private bool _fIsListBoxEnabled = true;
          public bool IsListBoxEnabled
          {
             get { return _fIsListBoxEnabled; }
             set
             {
                _fIsListBoxEnabled = value;
                OnPropertyChanged("IsListBoxEnabled");
             }
          }
    
          private void Button_Click(object sender, RoutedEventArgs e)
          {
             IsListBoxEnabled = !IsListBoxEnabled;
          }
    
          public ListBoxIsEnabled()
          {
             InitializeComponent();
          }
    
    
          #region INotifyPropertyChanged Members
    
          public event PropertyChangedEventHandler PropertyChanged;
          private void OnPropertyChanged(string prop)
          {
             if (PropertyChanged != null)
             {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
             }
          }
    
          #endregion
    
       }
    }
    
    



    Cheers,
    Olaf
  • Sunday, November 08, 2009 12:14 PMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Yehuda,

    yes, that'd be an alternative approach. Just replace the code-behind of my previous sample with this one here:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    namespace WpfTests
    {
       /// <summary>
       /// Interaction logic for ListBoxIsEnabled.xaml
       /// </summary>
       public partial class ListBoxIsEnabled : Window
       {
          public static DependencyProperty IsListBoxEnabledProperty =
             DependencyProperty.Register(
                   "IsListBoxEnabled",
                   typeof(bool),
                   typeof(ListBoxIsEnabled)
                );
    
          public bool IsListBoxEnabled
          {
             get { return (bool)GetValue(IsListBoxEnabledProperty); }
             set { SetValue(IsListBoxEnabledProperty, value); }
          }
    
          private void Button_Click(object sender, RoutedEventArgs e)
          {
             IsListBoxEnabled = !IsListBoxEnabled;
          }
    
          public ListBoxIsEnabled()
          {
             InitializeComponent();
          }
       }
    }
    
    



    Cheers,
    Olaf
    • Marked As Answer byYehuda A Saturday, November 21, 2009 5:20 PM
    •  

All Replies

  • Saturday, November 07, 2009 11:53 AMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Yehuda,

    to make this work, you'll have to notify the View that the isProductListEnabled property has changed. To do so, implement INotifyPropertyChanged in your ViewModel. Here's a sample that shows how to work this out; I didn't follow MVVM here for the sake of simplicity:

    <Window x:Class="WpfTests.ListBoxIsEnabled"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="ListBoxIsEnabled" Height="300" Width="300"
       DataContext="{Binding RelativeSource={RelativeSource Self}}"
       >
    
       <Grid>
          <Grid.RowDefinitions>
             <RowDefinition Height="Auto"/>
             <RowDefinition Height="*"/>
             <RowDefinition Height="Auto"/>
          </Grid.RowDefinitions>
          <Button Grid.Row="0" Content="Toggle ListBox'es IsEnabled-prop" Click="Button_Click" Margin="5" Padding="5"/>
          <ListBox Grid.Row="1" Foreground="OrangeRed" IsEnabled="{Binding IsListBoxEnabled}" Margin="5" SelectedIndex="0">
             <ListBoxItem Content="Item1"/>
             <ListBoxItem Content="Item2"/>
          </ListBox>
          <CheckBox Grid.Row="2" Content="ListBox is enabled" IsChecked="{Binding IsListBoxEnabled}" Margin="5"/>
       </Grid>
    </Window>
    
    

    And the code-behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.ComponentModel;
    
    namespace WpfTests
    {
       /// <summary>
       /// Interaction logic for ListBoxIsEnabled.xaml
       /// </summary>
       public partial class ListBoxIsEnabled : Window, INotifyPropertyChanged
       {
    
          private bool _fIsListBoxEnabled = true;
          public bool IsListBoxEnabled
          {
             get { return _fIsListBoxEnabled; }
             set
             {
                _fIsListBoxEnabled = value;
                OnPropertyChanged("IsListBoxEnabled");
             }
          }
    
          private void Button_Click(object sender, RoutedEventArgs e)
          {
             IsListBoxEnabled = !IsListBoxEnabled;
          }
    
          public ListBoxIsEnabled()
          {
             InitializeComponent();
          }
    
    
          #region INotifyPropertyChanged Members
    
          public event PropertyChangedEventHandler PropertyChanged;
          private void OnPropertyChanged(string prop)
          {
             if (PropertyChanged != null)
             {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
             }
          }
    
          #endregion
    
       }
    }
    
    



    Cheers,
    Olaf
  • Saturday, November 07, 2009 6:15 PMYehuda A Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Olaf
    Thank you very much
    It's working!

    Apparently I was only missing this line:
    OnPropertyChanged("IsListBoxEnabled");
     (in the 'set' of  IsListBoxEnabled property...)

    b.t.w. just for general knowledge,
    could I use for the same scenario with DependencyProperty?

    Again,
    Thanks - Yehuda
  • Sunday, November 08, 2009 12:14 PMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Yehuda,

    yes, that'd be an alternative approach. Just replace the code-behind of my previous sample with this one here:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    namespace WpfTests
    {
       /// <summary>
       /// Interaction logic for ListBoxIsEnabled.xaml
       /// </summary>
       public partial class ListBoxIsEnabled : Window
       {
          public static DependencyProperty IsListBoxEnabledProperty =
             DependencyProperty.Register(
                   "IsListBoxEnabled",
                   typeof(bool),
                   typeof(ListBoxIsEnabled)
                );
    
          public bool IsListBoxEnabled
          {
             get { return (bool)GetValue(IsListBoxEnabledProperty); }
             set { SetValue(IsListBoxEnabledProperty, value); }
          }
    
          private void Button_Click(object sender, RoutedEventArgs e)
          {
             IsListBoxEnabled = !IsListBoxEnabled;
          }
    
          public ListBoxIsEnabled()
          {
             InitializeComponent();
          }
       }
    }
    
    



    Cheers,
    Olaf
    • Marked As Answer byYehuda A Saturday, November 21, 2009 5:20 PM
    •