Ask a questionAsk a question
 

AnswerWPF Binding result not being retained

  • Wednesday, November 04, 2009 3:55 PMZest4Quest Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Could someone tell me what i am missing here..i am adding all the code required below...When i check the check boxes the binding does fire and set the Source value..but when i click the button and check the value the value is not true eventhough the check box is checked..
    Thanks in advance..

    namespace Window1
    {
        public struct Opt
        {
            private string _OptionsName;
            private bool _IsSelected;

            public Opt(string Opt, bool IsSelected)
                : this()
            {
                _OptionsName = Opt; _IsSelected = IsSelected;
            }
            public string OptionsName
            {
                get { return this._OptionsName; }
                set
                {
                    this._OptionsName = value;
                }
            }
            public bool IsSelected
            {
                get { return this._IsSelected; }
                set
                {
                    this._IsSelected = value;               
                }
            }
        }

        public class Options : INotifyPropertyChanged
        {
            #region Private members
            private string _Field;
            private List<Opt> _Filters;       
            #endregion

            #region Public Properties
            public string Field
            {
                get { return _Field; }
                set
                {
                    _Field = value;
                    NotifyPropertyChanged("Field");
                }
            }

            public List<Opt> Filters
            {
                get { return _Filters; }
                set
                {
                    _Filters = value;
                    NotifyPropertyChanged("Filters");
                }
            }
           
            #endregion

            #region Constructor
            public Options()
            {
                this._Filters = new List<Opt>();
            }
            #endregion

            #region Public Methods
            public void Load(List<string> Options)
            {
                if (Options != null && Options.Count > 0)
                {
                    foreach (string sFilter in Options)
                    {
                        Opt Opt = new Opt(sFilter, false);                   
                        this._Filters.Add(Opt);
                    }
                }
            }
           
            #endregion

            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;

            private void NotifyPropertyChanged(String FieldName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(FieldName));
                }
            }

            #endregion


        }
    }


    ///////////////////////////////////////////
    <Grid>
            <StackPanel Orientation="Horizontal">
            <Label Content="Field" Height="50" Width="200"></Label>
            <ComboBox Name="cmb" ItemsSource="{Binding Filters}" Height="30">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding OptionsName}" IsChecked="{Binding IsSelected}"></CheckBox>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
                <Button Name="btn" Click="btn_Click" Height="40">jjjj</Button>
                </StackPanel>
        </Grid>

    /////////////////////////////////////////
    public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
            Options filters = new Options();
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                List<string> values = new List<string>();
                values.Add("AAA");
                values.Add("BBB");
                values.Add("BBB");

               
                filters.Field = "Heading";
                filters.Load(values);
                this.cmb.DataContext = filters;
            }

            private void btn_Click(object sender, RoutedEventArgs e)
            {
                string s = filters.Field;
            }
        }

Answers

  • Wednesday, November 04, 2009 5:30 PMhbarck Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    this is a nice one. The problem is that, because structs are value types, what the combobox changes is actually copies of your listed Opts, not the Opts themselves. Change the declaration to class and it works...

    By the way, the change notification for Options.Filters doesn't work, because it only fires when a new instance of the list is assigned, and the items in the list themselves don't provide change notification. However, you might not need that anyway...
    • Marked As Answer byZest4Quest Wednesday, November 04, 2009 9:27 PM
    •  

All Replies

  • Wednesday, November 04, 2009 5:30 PMhbarck Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    this is a nice one. The problem is that, because structs are value types, what the combobox changes is actually copies of your listed Opts, not the Opts themselves. Change the declaration to class and it works...

    By the way, the change notification for Options.Filters doesn't work, because it only fires when a new instance of the list is assigned, and the items in the list themselves don't provide change notification. However, you might not need that anyway...
    • Marked As Answer byZest4Quest Wednesday, November 04, 2009 9:27 PM
    •  
  • Wednesday, November 04, 2009 9:27 PMZest4Quest Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Awesome! Thanks a lot hbarck!