Binding To Single List Item
-
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.
-
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.
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"
-
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.
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
-
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"
There's nothing there that says it's expecting a DP.
This space for rent
-
There's nothing there that says it's expecting a DP.
This space for rent
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"
-
There's nothing there that says it's expecting a DP.
This space for rent
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"
-
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"
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
-
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"
As explained below, it looks like he's using INPC so this is completely irrelevant.
This space for rent
-
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
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"
-
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"
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
-
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.
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