0
votes

Hello stackoverflowers,

I would like to set property canvas.left on the ListBoxItems of my ListBox. I'm trying this :

 <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Canvas.Left" Value="{Binding Content.StartPoint.X, Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
                        <Setter Property="Canvas.Top" Value="{Binding Content.StartPoint.Y}"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ListBoxItem">
                                    <ContentPresenter/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
</ListBox.ItemContainerStyle>

But it says it can't resolve content property in my datacontext.

I tried

<Setter Property="Canvas.Left" Value="{Binding Content.StartPoint.X}, Mode=TwoWay, RelativeSource={RelativeSource Self}}/>

But then it can't resolve StartPoint in datacontext object.

So i wonder how can i bind to the properties of my listbox items ??

EDIT : I'm binding ListBox ItemsSource to an ObservableCollection. The view is a UserControl.

EDIT 2 :

My listbox is binded to an observable collection of objects derivated from Foo.

Foo object defines StartPoint.

 public abstract class Foo: INotifyPropertyChanged
    {
      protected Point m_startPoint;

      public double Height { get; set; }

      public double Width { get; set; }

      public Point StartPoint
      {
         get { return m_startPoint; }
         set
         {
            m_startPoint = value;

            Width = Math.Abs(m_startPoint.X - m_endPoint.X);
            Height = Math.Abs(m_startPoint.Y - m_endPoint.Y);

            OnPropertyChanged(nameof(StartPoint));
            OnPropertyChanged(nameof(Width));
            OnPropertyChanged(nameof(Height));
        }
     }
1
Where you have Content? is it inside the DataSource object of ListView? or ViewModel of the view?Gopichandar
The content is in the view model of the view. I'm binding ListBox ItemsSource to an ObservableCollection.Csi
What is your typeof view? is it a UserControl or WindowGopichandar
I misunderstood your first comment. "Content" comes from System.Windows.Controls.ContentControl which ListBoxItem inherit from. I have an ObservableCollection binded to the Listbox in the ViewModel, and my View is a UserControl.Csi
You have to tell us where tthe Content.StartPoint.X resides.Gopichandar

1 Answers

0
votes

Provided that your ListBox is actually bound to a collection of Foo (or derived) objects, the bindings should look like this:

<Setter Property="Canvas.Left" Value="{Binding StartPoint.X}"/>
<Setter Property="Canvas.Top" Value="{Binding StartPoint.Y}"/>

The reason is that the DataContext of each ListBoxItem is set to the corresponding data item from the ItemsSource collection, i.e. the corresponding Foo instance.