简体   繁体   English

绑定到ListBox ItemContainerStyle中的ListBoxItem属性

[英]Binding to ListBoxItem properties in ListBox ItemContainerStyle

Hello stackoverflowers, 你好stackoverflowers,

I would like to set property canvas.left on the ListBoxItems of my ListBox. 我想在ListBox的ListBoxItems上设置属性canvas.left。 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. 但是它说它无法解析我的datacontext中的content属性。

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. 但是,它无法解析datacontext对象中的StartPoint。

So i wonder how can i bind to the properties of my listbox items ?? 所以我想知道如何绑定到列表框项目的属性?

EDIT : I'm binding ListBox ItemsSource to an ObservableCollection. 编辑:我将ListBox ItemsSource绑定到ObservableCollection。 The view is a UserControl. 该视图是一个UserControl。

EDIT 2 : 编辑2:

My listbox is binded to an observable collection of objects derivated from Foo. 我的列表框绑定到Foo派生的可观察对象集合。

Foo object defines StartPoint. Foo对象定义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));
        }
     }

Provided that your ListBox is actually bound to a collection of Foo (or derived) objects, the bindings should look like this: 如果您的ListBox实际上绑定到Foo(或派生)对象的集合,则绑定应如下所示:

<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, ie the corresponding Foo instance. 原因是每个ListBoxItem的DataContext设置为ItemsSource集合中的相应数据项,即对应的Foo实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM