fperks
fperks

Reputation: 23

ItemsControl Binding the Current Object?

I am just started learning WPF, and trying to learn the correct way to do things. I am using a ItemsControl to bind to an Observable Collection consisting of Points. Each point represents the center of an ellipse. However I am unsure how to bind to the current item, to the center property of the Ellipse Geometry.

<EllipseGeometry Center="{Binding Path=????}" RadiusX="10" RadiusY="10"/>

However I know it is more or less working, since this gives me the expected output. I just can't figure out how to say the current object.

<EllipseGeometry Center="10,10" RadiusX="{Binding Path=X}" RadiusY="{Binding Path=Y}"/>

C# Code:

public class ViewModel
{
    public ObservableCollection<Point> PointList { get; private set; }

    public ViewModel()
    {
        PointList = new ObservableCollection<Point>();
        AddPoint(new Point(10, 10));
        AddPoint(new Point(200, 200));
        AddPoint(new Point(500, 500));
    }

    public void AddPoint(Point p)
    {
        PointList.Add(p);
    }

}

XAML Code:

<ItemsControl Grid.Row="0" ItemsSource="{Binding Path=PointList}">
    <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Canvas />
    </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
    <DataTemplate>
        <Path Fill="Gold" Stroke="Black" StrokeThickness="1" Tag="Hi" >
        <Path.Data>
            <EllipseGeometry Center="{Binding Path=????}" RadiusX="10" RadiusY="10"/>
        </Path.Data>
        </Path>
        <!--<Rectangle Fill="Red" Width="25" Height="25"/>-->
    </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemContainerStyle>
    <Style>
        <!--<Setter Property="Canvas.Left" Value="{Binding Path=X}"/>
        <Setter Property="Canvas.Top" Value="{Binding Path=Y}"/>-->
    </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

Upvotes: 2

Views: 3129

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178810

If you leave out the path entirely, it refers to the current object. Alternatively, you can specify a period. Thus, all these are equivalent:

Center="{Binding}"
Center="{Binding .}"
Center="{Binding Path=.}"

Upvotes: 7

Related Questions