I have a Canvas inside an ItemsControl whose ItemsSource is an ObservableCollection. Withing this is a Rectangle whose Width property is bound to RectangleOverlay's wWdith property. Width appears 0 no matter what I have tried & I have tried moving things around. Here is the View.
<Grid>
<Label HorizontalAlignment="Center"
Content="IMAGE FROM CAMERA"
Width="Auto" FontFamily="White"
Canvas.Left="50" Panel.ZIndex="2"/>
<ItemsControl Panel.ZIndex="3"
ItemsSource="{Binding Source=MyProperty}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas Height="Auto" >
<Rectangle Width="{Binding Path=wWidth,
UpdateSourceTrigger=PropertyChanged}"
Height="50"
Canvas.Left="50"
Canvas.Top="50"
Stroke="Red"
StrokeThickness="2"/>
<TextBox Canvas.Left="50"
Canvas.Top="100"
Text="TEXT"/>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
And here is the code for RectangleOverlay class
public class RectOverlay : Shape, INotifyPropertyChanged
{
public RectOverlay()
{
}
//I DONT KNOW WHAT EXACTLY NEEDS TO BE IN HERE
protected override Geometry DefiningGeometry
{
get { return new RectangleGeometry();}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
private double _width;
public double wWidth
{
get { return _width; }
set
{
_width = value;
OnPropertyChanged("wWidth");
}
}
}
And here is most of the code behind(MainWindow.xaml.cs).
public partial class MainWindow : Window, INotifyPropertyChanged
{
private ObservableCollection<RectOverlay> ListShapes;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
MyProperty = new ObservableCollection<RectOverlay>();
ListShapes.Add(new RectOverlay() { wWidth = 100 });
_timer = new Timer(2000); // Set up the timer for 3 seconds
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Enabled = true; // Enable it
}
public ObservableCollection<RectOverlay> MyProperty
{
get { return ListShapes; }
set
{
ListShapes = value;
OnPropertyChanged("MyProperty");
}
}
}
I cannot seem to understanding why the binding fails.
System.Windows.Data Error: 40 : BindingExpression path error: 'wWidth' property not found on 'object' ''Char' (HashCode=5046349)'. BindingExpression:Path=wWidth; DataItem='Char' (HashCode=5046349); target element is 'Rectangle' (Name=''); target property is 'Width' (type 'Double')
I have read & tried to figure out from other similar posts on SO about how to infer this error. I think I have the binding flow correctly set. The data context of the entire window is the code behind, there after ItemsControl Source is the ObservableCollection of a custom class(RectangleOverlay). & inside Data Template, I am setting path to a property of my Custom class.