I\'m trying to bind a TextBlock to a specific element in an ObservableCollection. This is what I do right now:
private ObservableCollection arr
you can use this way in my case I want to binding the visibility from an boolean array: code behind:
using System.Windows;
public static readonly DependencyProperty ButtonVisibleProperty =
DependencyProperty.Register("ButtonVisible", typeof(BindingList<Boolean>), typeof(BaseWindow), new PropertyMetadata(new BindingList<Boolean>()));
public BindingList<Boolean> ButtonVisible
{
get { return (BindingList<Boolean>)GetValue(BaseWindow.ButtonVisibleProperty); }
set
{
SetValue(BaseWindow.ButtonVisibleProperty, value);
OnPropertyChanged("ButtonVisible");
}
}
and in Xaml:
Visibility=""{Binding Path=ButtonVisible[0], RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
beware! the ancestorType depend of your Ancestor in my case is a window
ObservableCollection
s do not propagate changes to values stored within objects that belong to the collection. It only fires off the notifications when the content of the collection itself changes (ie. item added, removed, reordered). If you want to make it so that your UI updates when values within the collection changes then you'll have to wire that up yourself separately.
No need for the converter. You can bind directly to Arr[0]
like this
<TextBlock Name="testBox" Text="{Binding Path=Arr[0]}"/>
The elements in Arr
would need to implement INotifyPropertyChanged
though in order to dynamically update.
Update: To elaborate a bit more:
public class MyDouble : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private double _Value;
public double Value
{
get { return _Value; }
set { _Value = value; OnPropertyChanged("Value"); }
}
void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
and then
ObservableCollection<MyDouble> Arr { get; set; }
and bind to
<TextBlock Name="testBox" Text="{Binding Path=Arr[0].Value}"/>