Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Binding To Single List Item

Binding To Single List Item

Scheduled Pinned Locked Moved WPF
wpfhelpdatabasewcfquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have a list of booleans, and I want to bind checkboxes to this list.\ Code Behind

    private List _OnOffValues;
    public List OnOffValues
    {
    get { return _OnOffValues; }
    set
    {
    if (_OnOffValues != value)
    {
    _OnOffValues = value;
    RaisePropertyChanged("OnOffValues");
    }
    }
    }
    public MyControl()
    {
    InitializeComponent();
    this.DataContext = this;
    OnOffValues = new List();
    for (int x = 0; x < 24; x++)
    {
    OnOffValues.Add(true);
    }
    }

    XAML

    The output window shows

    System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Boolean') from 'OnOffValues' (type 'List`1'). BindingExpression:Path=OnOffValues[0]; DataItem='MyControl' (Name='control'); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
    Parameter name: index'

    The code in the contructor runs and the list is populated, but the output message appears BEFORE that. What am I doing wrong here??

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    M P L 3 Replies Last reply
    0
    • K Kevin Marois

      I have a list of booleans, and I want to bind checkboxes to this list.\ Code Behind

      private List _OnOffValues;
      public List OnOffValues
      {
      get { return _OnOffValues; }
      set
      {
      if (_OnOffValues != value)
      {
      _OnOffValues = value;
      RaisePropertyChanged("OnOffValues");
      }
      }
      }
      public MyControl()
      {
      InitializeComponent();
      this.DataContext = this;
      OnOffValues = new List();
      for (int x = 0; x < 24; x++)
      {
      OnOffValues.Add(true);
      }
      }

      XAML

      The output window shows

      System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Boolean') from 'OnOffValues' (type 'List`1'). BindingExpression:Path=OnOffValues[0]; DataItem='MyControl' (Name='control'); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
      Parameter name: index'

      The code in the contructor runs and the list is populated, but the output message appears BEFORE that. What am I doing wrong here??

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      M Offline
      M Offline
      Meshack Musundi
      wrote on last edited by
      #2

      Your binding assumes that OnOffValues is a dependency property of the control.

      "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

      P 1 Reply Last reply
      0
      • K Kevin Marois

        I have a list of booleans, and I want to bind checkboxes to this list.\ Code Behind

        private List _OnOffValues;
        public List OnOffValues
        {
        get { return _OnOffValues; }
        set
        {
        if (_OnOffValues != value)
        {
        _OnOffValues = value;
        RaisePropertyChanged("OnOffValues");
        }
        }
        }
        public MyControl()
        {
        InitializeComponent();
        this.DataContext = this;
        OnOffValues = new List();
        for (int x = 0; x < 24; x++)
        {
        OnOffValues.Add(true);
        }
        }

        XAML

        The output window shows

        System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Boolean') from 'OnOffValues' (type 'List`1'). BindingExpression:Path=OnOffValues[0]; DataItem='MyControl' (Name='control'); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
        Parameter name: index'

        The code in the contructor runs and the list is populated, but the output message appears BEFORE that. What am I doing wrong here??

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        If you check the timing of what's happening, the bind is happening as the control is being created - it happens in the InitializeComponent call in the user control. What you could do here is supply a FallbackValue so that the binding is satisfied until the point that your ViewModel is created.

        This space for rent

        1 Reply Last reply
        0
        • M Meshack Musundi

          Your binding assumes that OnOffValues is a dependency property of the control.

          "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          There's nothing there that says it's expecting a DP.

          This space for rent

          M 2 Replies Last reply
          0
          • P Pete OHanlon

            There's nothing there that says it's expecting a DP.

            This space for rent

            M Offline
            M Offline
            Meshack Musundi
            wrote on last edited by
            #5

            Look at his binding, IsChecked="{Binding OnOffValues[0], ElementName=control}" and the property, OnOffValues, is in the code behind of his user control. If he changes the property to a dependency property he won't get the error.

            "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

            P 1 Reply Last reply
            0
            • P Pete OHanlon

              There's nothing there that says it's expecting a DP.

              This space for rent

              M Offline
              M Offline
              Meshack Musundi
              wrote on last edited by
              #6

              Something like this, Code behind

              public partial class SomeControl : UserControl
              {
              public SomeControl()
              {
              InitializeComponent();
              for (int i = 0; i < 20; i++)
              {
              OnOffValues.Add(true);
              }
              }

              public List<bool> OnOffValues
              {
                  get { return (List<bool>)GetValue(OnOffValuesProperty); }
                  set { SetValue(OnOffValuesProperty, value); }
              }
              
              public static readonly DependencyProperty OnOffValuesProperty =
                  DependencyProperty.Register("OnOffValues", typeof(List<bool>),
                      typeof(SomeControl), new PropertyMetadata(new List<bool>()));
              

              }

              XAML

              <CheckBox IsChecked="{Binding Path=OnOffValues[0], RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>

              "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

              P 1 Reply Last reply
              0
              • M Meshack Musundi

                Something like this, Code behind

                public partial class SomeControl : UserControl
                {
                public SomeControl()
                {
                InitializeComponent();
                for (int i = 0; i < 20; i++)
                {
                OnOffValues.Add(true);
                }
                }

                public List<bool> OnOffValues
                {
                    get { return (List<bool>)GetValue(OnOffValuesProperty); }
                    set { SetValue(OnOffValuesProperty, value); }
                }
                
                public static readonly DependencyProperty OnOffValuesProperty =
                    DependencyProperty.Register("OnOffValues", typeof(List<bool>),
                        typeof(SomeControl), new PropertyMetadata(new List<bool>()));
                

                }

                XAML

                <CheckBox IsChecked="{Binding Path=OnOffValues[0], RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>

                "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                He's set the UserControl as its own DataContext. He doesn't need to create a DP if he's set the UC to implement INotifyPropertyChanged (which the property change notification suggests).

                This space for rent

                M 1 Reply Last reply
                0
                • M Meshack Musundi

                  Look at his binding, IsChecked="{Binding OnOffValues[0], ElementName=control}" and the property, OnOffValues, is in the code behind of his user control. If he changes the property to a dependency property he won't get the error.

                  "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  As explained below, it looks like he's using INPC so this is completely irrelevant.

                  This space for rent

                  1 Reply Last reply
                  0
                  • P Pete OHanlon

                    He's set the UserControl as its own DataContext. He doesn't need to create a DP if he's set the UC to implement INotifyPropertyChanged (which the property change notification suggests).

                    This space for rent

                    M Offline
                    M Offline
                    Meshack Musundi
                    wrote on last edited by
                    #9

                    Quite true, I had taken note of that. I only supposed that the more apt approach would be to use a dependency property.

                    "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                    P 1 Reply Last reply
                    0
                    • M Meshack Musundi

                      Quite true, I had taken note of that. I only supposed that the more apt approach would be to use a dependency property.

                      "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      Not when he already set the DataContext. The issue was purely a timing issue and could be fixed with a FallbackValue.

                      This space for rent

                      1 Reply Last reply
                      0
                      • K Kevin Marois

                        I have a list of booleans, and I want to bind checkboxes to this list.\ Code Behind

                        private List _OnOffValues;
                        public List OnOffValues
                        {
                        get { return _OnOffValues; }
                        set
                        {
                        if (_OnOffValues != value)
                        {
                        _OnOffValues = value;
                        RaisePropertyChanged("OnOffValues");
                        }
                        }
                        }
                        public MyControl()
                        {
                        InitializeComponent();
                        this.DataContext = this;
                        OnOffValues = new List();
                        for (int x = 0; x < 24; x++)
                        {
                        OnOffValues.Add(true);
                        }
                        }

                        XAML

                        The output window shows

                        System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Boolean') from 'OnOffValues' (type 'List`1'). BindingExpression:Path=OnOffValues[0]; DataItem='MyControl' (Name='control'); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
                        Parameter name: index'

                        The code in the contructor runs and the list is populated, but the output message appears BEFORE that. What am I doing wrong here??

                        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Changing / setting the DataContext invokes the binding mechanism.

                        public MyControl()
                        {
                        OnOffValues = new List();
                        for (int x = 0; x < 24; x++)
                        {
                        OnOffValues.Add(true);
                        }

                        InitializeComponent();
                        this.DataContext = this;
                        

                        }

                        And there is no "ElementName" in play here.

                        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups